﻿using System.Collections.Generic;
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Web.DynamicData;
using System.Data.SqlClient;
using System.Globalization;

namespace sistemaAcademicoFinanceiroEscolas
{
    public partial class agendaProfessorMaior2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                SqlConnection sqlc = new SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
                sqlc.Open();

                string sqlmes = "select * from mes";

                SqlCommand cmdmes = new SqlCommand(sqlmes, sqlc);
                SqlDataReader myReadermes = cmdmes.ExecuteReader();

                while (myReadermes.Read())
                {
                    ListItem mesSelecione = new ListItem();
                    mesSelecione.Value = myReadermes["codigomes"].ToString();
                    mesSelecione.Text = myReadermes["descricaomes"].ToString();
                    lstMes.Items.Add(mesSelecione);
                }
                cmdmes.Dispose();
                myReadermes.Close();

                string sqlAnoLetivo = "select * from anoLetivo";

                SqlCommand cmdAnoLetivo = new SqlCommand(sqlAnoLetivo, sqlc);
                SqlDataReader myReaderAnoLetivo = cmdAnoLetivo.ExecuteReader();

                while (myReaderAnoLetivo.Read())
                {
                    ListItem anoLetivoSelecione = new ListItem();
                    anoLetivoSelecione.Value = myReaderAnoLetivo["codigoAnoLetivo"].ToString();
                    anoLetivoSelecione.Text = myReaderAnoLetivo["descricaoAnoLetivo"].ToString();
                    if (DateTime.Now.Year.ToString() == myReaderAnoLetivo["descricaoAnoLetivo"].ToString())
                    {
                        anoLetivoSelecione.Selected = true;
                    }
                    lstAnoLetivo.Items.Add(anoLetivoSelecione);
                }
                cmdAnoLetivo.Dispose();
                myReaderAnoLetivo.Close();

                string sqlProfessor = "select * from professor order by nomeprofessor";

                SqlCommand cmdProfessor = new SqlCommand(sqlProfessor, sqlc);
                SqlDataReader myReaderProfessor = cmdProfessor.ExecuteReader();

                while (myReaderProfessor.Read())
                {
                    ListItem ProfessorSelecione = new ListItem();
                    ProfessorSelecione.Value = myReaderProfessor["codigoProfessor"].ToString();
                    ProfessorSelecione.Text = myReaderProfessor["nomeProfessor"].ToString();
                    lstProfessor.Items.Add(ProfessorSelecione);
                }
                cmdProfessor.Dispose();
                myReaderProfessor.Close();

                string sqlDia = "select * from dia";

                SqlCommand cmdDia = new SqlCommand(sqlDia, sqlc);
                SqlDataReader myReaderDia = cmdDia.ExecuteReader();

                while (myReaderDia.Read())
                {
                    ListItem DiaSelecione = new ListItem();
                    DiaSelecione.Value = myReaderDia["codigoDia"].ToString();
                    DiaSelecione.Text = myReaderDia["dia"].ToString();
                    lstDia.Items.Add(DiaSelecione);
                }
                cmdDia.Dispose();
                myReaderDia.Close();

                sqlc.Close();

                lstDia.SelectedValue = DateTime.Now.Day.ToString();
                lstMes.SelectedValue = DateTime.Now.Month.ToString();
            }

            txt1.Attributes.Add("onfocusout", "algumaFuncaoJavascript");
        }

        protected void lstMes_SelectedIndexChanged(object sender, EventArgs e)
        {
            lstDia.Items.Clear();

            string mes = lstMes.SelectedValue;
            if (mes.Length == 1)
            {
                mes = "0" + lstMes.SelectedValue;
            }

            int pegarMes = Convert.ToInt16(mes);

            JulianCalendar cal = new JulianCalendar();

            int quantidadeDias = cal.GetDaysInMonth(1, pegarMes);

            int i = 1;
            for (i = 1; i <= quantidadeDias; i++)
            {
                ListItem dia = new ListItem();
                dia.Value = i.ToString();
                dia.Text = i.ToString();

                lstDia.Items.Add(dia);
            }
        }

        protected void btnVerificar_Click(object sender, EventArgs e)
        {

            //limpar os valores dos txts

            txt1.Text = "";
            txt2.Text = "";
            txt3.Text = "";
            txt4.Text = "";
            txt5.Text = "";
            txt6.Text = "";
            txt7.Text = "";
            txt8.Text = "";

            inicial1Hora1.Text = "";
            final1Hora1.Text = "";
            inicial2Hora2.Text = "";
            final2Hora2.Text = "";
            inicial3Hora3.Text = "";
            final3Hora3.Text = "";
            inicial4Hora4.Text = "";
            final4Hora4.Text = "";
            inicial5Hora5.Text = "";
            final5Hora5.Text = "";
            inicial6Hora6.Text = "";
            final6Hora6.Text = "";
            inicial7Hora7.Text = "";
            final7Hora7.Text = "";
            inicial8Hora8.Text = "";
            final8Hora8.Text = "";

            SqlConnection sqlc = new SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
            sqlc.Open();

            lstgrupo.Items.Clear();
            lstgrupo2.Items.Clear();
            lstgrupo3.Items.Clear();
            lstgrupo4.Items.Clear();
            lstgrupo5.Items.Clear();
            lstgrupo6.Items.Clear();
            lstgrupo7.Items.Clear();
            lstgrupo8.Items.Clear();

            lstmateria.Items.Clear();
            lstmateria2.Items.Clear();
            lstmateria3.Items.Clear();
            lstmateria4.Items.Clear();
            lstmateria5.Items.Clear();
            lstmateria6.Items.Clear();
            lstmateria7.Items.Clear();
            lstmateria8.Items.Clear();

            string sqlgrupo = "select * from grupo order by nomegrupo";

            SqlCommand cmdgrupo = new SqlCommand(sqlgrupo, sqlc);
            SqlDataReader myReadergrupo = cmdgrupo.ExecuteReader();

            ListItem primeiro = new ListItem();

            primeiro.Value = "0";
            primeiro.Text = "selecione";

            lstgrupo.Items.Add(primeiro);
            lstgrupo2.Items.Add(primeiro);
            lstgrupo3.Items.Add(primeiro);
            lstgrupo4.Items.Add(primeiro);
            lstgrupo5.Items.Add(primeiro);
            lstgrupo6.Items.Add(primeiro);
            lstgrupo7.Items.Add(primeiro);
            lstgrupo8.Items.Add(primeiro);

            while (myReadergrupo.Read())
            {
                ListItem grupoSelecione = new ListItem();

                ListItem grupoSelecione2 = new ListItem();

                ListItem grupoSelecione3 = new ListItem();

                ListItem grupoSelecione4 = new ListItem();

                ListItem grupoSelecione5 = new ListItem();

                ListItem grupoSelecione6 = new ListItem();

                ListItem grupoSelecione7 = new ListItem();

                ListItem grupoSelecione8 = new ListItem();

                grupoSelecione.Value = myReadergrupo["codigogrupo"].ToString();
                grupoSelecione.Text = myReadergrupo["nomegrupo"].ToString();

                grupoSelecione2.Value = myReadergrupo["codigogrupo"].ToString();
                grupoSelecione2.Text = myReadergrupo["nomegrupo"].ToString();

                grupoSelecione3.Value = myReadergrupo["codigogrupo"].ToString();
                grupoSelecione3.Text = myReadergrupo["nomegrupo"].ToString();

                grupoSelecione4.Value = myReadergrupo["codigogrupo"].ToString();
                grupoSelecione4.Text = myReadergrupo["nomegrupo"].ToString();

                grupoSelecione5.Value = myReadergrupo["codigogrupo"].ToString();
                grupoSelecione5.Text = myReadergrupo["nomegrupo"].ToString();

                grupoSelecione6.Value = myReadergrupo["codigogrupo"].ToString();
                grupoSelecione6.Text = myReadergrupo["nomegrupo"].ToString();

                grupoSelecione7.Value = myReadergrupo["codigogrupo"].ToString();
                grupoSelecione7.Text = myReadergrupo["nomegrupo"].ToString();

                grupoSelecione8.Value = myReadergrupo["codigogrupo"].ToString();
                grupoSelecione8.Text = myReadergrupo["nomegrupo"].ToString();

                lstgrupo.Items.Add(grupoSelecione);
                lstgrupo2.Items.Add(grupoSelecione2);
                lstgrupo3.Items.Add(grupoSelecione3);
                lstgrupo4.Items.Add(grupoSelecione4);
                lstgrupo5.Items.Add(grupoSelecione5);
                lstgrupo6.Items.Add(grupoSelecione6);
                lstgrupo7.Items.Add(grupoSelecione7);
                lstgrupo8.Items.Add(grupoSelecione8);
            }
            cmdgrupo.Dispose();
            myReadergrupo.Close();

            string sqlDiscProf = "select p.codigoProfessor as codigoProfessor, p.nomeProfessor as nomeProfessor, d.codigoDisciplina as codigoDisciplina, ";
            sqlDiscProf = sqlDiscProf + "d.descricaodisciplina as descricaodisciplina, d.codigoSerie, dp.codigoProfessor, dp.codigoDisciplina, s.descricaoSerie as descricaoSerie, s.codigoSerie as codigoSerie";
            sqlDiscProf = sqlDiscProf + " from professor p,";
            sqlDiscProf = sqlDiscProf + " disciplinaProfessor dp, disciplina d, serie s ";
            sqlDiscProf = sqlDiscProf + " where dp.codigoDisciplina = d.codigoDisciplina and ";
            sqlDiscProf = sqlDiscProf + " dp.codigoProfessor = p.codigoProfessor and ";
            sqlDiscProf = sqlDiscProf + " p.codigoProfessor = " + lstProfessor.SelectedValue + " and d.codigoSerie = s.codigoSerie order by d.descricaoDisciplina";

            SqlCommand cmddisciplina = new SqlCommand(sqlDiscProf, sqlc);
            SqlDataReader myReaderDisc = cmddisciplina.ExecuteReader();


            lstmateria.Items.Add(primeiro);
            lstmateria2.Items.Add(primeiro);
            lstmateria3.Items.Add(primeiro);
            lstmateria4.Items.Add(primeiro);
            lstmateria5.Items.Add(primeiro);
            lstmateria6.Items.Add(primeiro);
            lstmateria7.Items.Add(primeiro);
            lstmateria8.Items.Add(primeiro);

            if (myReaderDisc.HasRows)
            {
                while (myReaderDisc.Read())
                {
                    ListItem materia = new ListItem();
                    ListItem materia2 = new ListItem();
                    ListItem materia3 = new ListItem();
                    ListItem materia4 = new ListItem();
                    ListItem materia5 = new ListItem();
                    ListItem materia6 = new ListItem();
                    ListItem materia7 = new ListItem();
                    ListItem materia8 = new ListItem();

                    materia.Value = myReaderDisc["codigoDisciplina"].ToString();
                    materia.Text = myReaderDisc["descricaodisciplina"].ToString();

                    materia2.Value = myReaderDisc["codigoDisciplina"].ToString();
                    materia2.Text = myReaderDisc["descricaodisciplina"].ToString();

                    materia3.Value = myReaderDisc["codigoDisciplina"].ToString();
                    materia3.Text = myReaderDisc["descricaodisciplina"].ToString();

                    materia4.Value = myReaderDisc["codigoDisciplina"].ToString();
                    materia4.Text = myReaderDisc["descricaodisciplina"].ToString();

                    materia5.Value = myReaderDisc["codigoDisciplina"].ToString();
                    materia5.Text = myReaderDisc["descricaodisciplina"].ToString();

                    materia6.Value = myReaderDisc["codigoDisciplina"].ToString();
                    materia6.Text = myReaderDisc["descricaodisciplina"].ToString();

                    materia7.Value = myReaderDisc["codigoDisciplina"].ToString();
                    materia7.Text = myReaderDisc["descricaodisciplina"].ToString();

                    materia8.Value = myReaderDisc["codigoDisciplina"].ToString();
                    materia8.Text = myReaderDisc["descricaodisciplina"].ToString();

                    lstmateria.Items.Add(materia);
                    lstmateria2.Items.Add(materia2);
                    lstmateria3.Items.Add(materia3);
                    lstmateria4.Items.Add(materia4);
                    lstmateria5.Items.Add(materia5);
                    lstmateria6.Items.Add(materia6);
                    lstmateria7.Items.Add(materia7);
                    lstmateria8.Items.Add(materia8);
                }
            }

            myReaderDisc.Close();





            string dia = lstDia.SelectedValue;
            if (dia.Length == 1)
            {
                dia = "0" + lstDia.SelectedValue;
            }

            string mes = lstMes.SelectedValue;
            if (mes.Length == 1)
            {
                mes = "0" + lstMes.SelectedValue;
            }

            string ano = lstAnoLetivo.SelectedItem.Text;


            //o valor da hora deixou de ser necessário aqui ja que agora vem da tabela aula valor e uma vez que
            //os descontos dos alunos sao definidos em valorEspecialAluno
            //o campo valor aula na tabela aula deixou de ser necessário.



            System.Data.SqlClient.SqlConnection sqlj = new System.Data.SqlClient.SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
            sqlj.Open();

            string selectAula = "SELECT codigoAula, codigoProfessor, codigoAluno, codigoGrupo, dataAula, horaInicialAula, ";
            selectAula += "horaFinalAula, codigoDisciplina, descricaoAssuntoAula, observacoesAula, ";
            selectAula += "aulaRealizada, valorHora, valorTotalSemDesconto, valorTotalComDesconto, falta, codigoAnoLetivo, linha, ";

            selectAula += "(SELECT MIN(horaInicialAula) AS horaInicial FROM aula AS a WHERE (linha = '1') AND (codigoProfessor = " + lstProfessor.SelectedValue + ") AND (dataAula = '" + dia + "/" + mes + "/" + ano + "')) AS horaInicial, ";
            selectAula += "(SELECT MAX(horaFinalAula) AS horaFinal FROM aula AS aa WHERE (linha = '1') AND (codigoProfessor = " + lstProfessor.SelectedValue + ") AND (dataAula = '" + dia + "/" + mes + "/" + ano + "')) AS horaFinal, ";
            selectAula += "(SELECT MIN(horaInicialAula) AS horaInicial2 FROM aula AS a WHERE (linha = '2') AND (codigoProfessor = " + lstProfessor.SelectedValue + ") AND (dataAula = '" + dia + "/" + mes + "/" + ano + "')) AS horaInicial2, ";
            selectAula += "(SELECT MAX(horaFinalAula) AS horaFinal2 FROM aula AS aa WHERE (linha = '2') AND (codigoProfessor = " + lstProfessor.SelectedValue + ") AND (dataAula = '" + dia + "/" + mes + "/" + ano + "')) AS horaFinal2, ";
            selectAula += "(SELECT MIN(horaInicialAula) AS horaInicial3 FROM aula AS a WHERE (linha = '3') AND (codigoProfessor = " + lstProfessor.SelectedValue + ") AND (dataAula = '" + dia + "/" + mes + "/" + ano + "')) AS horaInicial3, ";
            selectAula += "(SELECT MAX(horaFinalAula) AS horaFinal3 FROM aula AS aa WHERE (linha = '3') AND (codigoProfessor = " + lstProfessor.SelectedValue + ") AND (dataAula = '" + dia + "/" + mes + "/" + ano + "')) AS horaFinal3, ";
            selectAula += "(SELECT MIN(horaInicialAula) AS horaInicial4 FROM aula AS a WHERE (linha = '4') AND (codigoProfessor = " + lstProfessor.SelectedValue + ") AND (dataAula = '" + dia + "/" + mes + "/" + ano + "')) AS horaInicial4, ";
            selectAula += "(SELECT MAX(horaFinalAula) AS horaFinal4 FROM aula AS aa WHERE (linha = '4') AND (codigoProfessor = " + lstProfessor.SelectedValue + ") AND (dataAula = '" + dia + "/" + mes + "/" + ano + "')) AS horaFinal4, ";
            selectAula += "(SELECT MIN(horaInicialAula) AS horaInicial5 FROM aula AS a WHERE (linha = '5') AND (codigoProfessor = " + lstProfessor.SelectedValue + ") AND (dataAula = '" + dia + "/" + mes + "/" + ano + "')) AS horaInicial5, ";
            selectAula += "(SELECT MAX(horaFinalAula) AS horaFinal5 FROM aula AS aa WHERE (linha = '5') AND (codigoProfessor = " + lstProfessor.SelectedValue + ") AND (dataAula = '" + dia + "/" + mes + "/" + ano + "')) AS horaFinal5, ";
            selectAula += "(SELECT MIN(horaInicialAula) AS horaInicial6 FROM aula AS a WHERE (linha = '6') AND (codigoProfessor = " + lstProfessor.SelectedValue + ") AND (dataAula = '" + dia + "/" + mes + "/" + ano + "')) AS horaInicial6, ";
            selectAula += "(SELECT MAX(horaFinalAula) AS horaFinal6 FROM aula AS aa WHERE (linha = '6') AND (codigoProfessor = " + lstProfessor.SelectedValue + ") AND (dataAula = '" + dia + "/" + mes + "/" + ano + "')) AS horaFinal6, ";
            selectAula += "(SELECT MIN(horaInicialAula) AS horaInicial7 FROM aula AS a WHERE (linha = '7') AND (codigoProfessor = " + lstProfessor.SelectedValue + ") AND (dataAula = '" + dia + "/" + mes + "/" + ano + "')) AS horaInicial7, ";
            selectAula += "(SELECT MAX(horaFinalAula) AS horaFinal7 FROM aula AS aa WHERE (linha = '7') AND (codigoProfessor = " + lstProfessor.SelectedValue + ") AND (dataAula = '" + dia + "/" + mes + "/" + ano + "')) AS horaFinal7, ";
            selectAula += "(SELECT MIN(horaInicialAula) AS horaInicial8 FROM aula AS a WHERE (linha = '8') AND (codigoProfessor = " + lstProfessor.SelectedValue + ") AND (dataAula = '" + dia + "/" + mes + "/" + ano + "')) AS horaInicial8, ";
            selectAula += "(SELECT MAX(horaFinalAula) AS horaFinal8 FROM aula AS aa WHERE (linha = '8') AND (codigoProfessor = " + lstProfessor.SelectedValue + ") AND (dataAula = '" + dia + "/" + mes + "/" + ano + "')) AS horaFinal8, ";
            selectAula += "(SELECT nomeAluno FROM aluno WHERE (codigoAluno = aula.codigoAluno)) AS aluno ";

            if (dia.Length == 1)
            {
                dia = "0" + dia;
            }
            if (mes.Length == 1)
            {
                mes = "0" + mes;
            }

            selectAula += "FROM aula WHERE (codigoProfessor = " + lstProfessor.SelectedValue + ") AND (dataAula = '" + dia + "/" + mes + "/" + ano + "')";

            System.Data.SqlClient.SqlCommand cmdselectAula = new System.Data.SqlClient.SqlCommand(selectAula, sqlj);
            System.Data.SqlClient.SqlDataReader drselectAula = cmdselectAula.ExecuteReader();

            string inicial1Hora = "";
            string final1Hora = "";

            string inicial2Hora = "";
            string final2Hora = "";

            string inicial3Hora = "";
            string final3Hora = "";

            string inicial4Hora = "";
            string final4Hora = "";

            string inicial5Hora = "";
            string final5Hora = "";

            string inicial6Hora = "";
            string final6Hora = "";

            string inicial7Hora = "";
            string final7Hora = "";

            string inicial8Hora = "";
            string final8Hora = "";

            if (drselectAula.HasRows)
            {
                while (drselectAula.Read())
                {
                    inicial1Hora = drselectAula["horaInicial"].ToString();
                    final1Hora = drselectAula["horaFinal"].ToString();

                    inicial2Hora = drselectAula["horaInicial2"].ToString();
                    final2Hora = drselectAula["horaFinal2"].ToString();

                    inicial3Hora = drselectAula["horaInicial3"].ToString();
                    final3Hora = drselectAula["horaFinal3"].ToString();

                    inicial4Hora = drselectAula["horaInicial4"].ToString();
                    final4Hora = drselectAula["horaFinal4"].ToString();

                    inicial5Hora = drselectAula["horaInicial5"].ToString();
                    final5Hora = drselectAula["horaFinal5"].ToString();

                    inicial6Hora = drselectAula["horaInicial6"].ToString();
                    final6Hora = drselectAula["horaFinal6"].ToString();

                    inicial7Hora = drselectAula["horaInicial7"].ToString();
                    final7Hora = drselectAula["horaFinal7"].ToString();

                    inicial8Hora = drselectAula["horaInicial8"].ToString();
                    final8Hora = drselectAula["horaFinal8"].ToString();

                    switch (drselectAula["linha"].ToString())
                    {
                        case "1":
                            if (inicial1Hora != "" && final1Hora != "")
                            {
                                inicial1Hora1.Text = inicial1Hora;
                                final1Hora1.Text = final1Hora;
                                if (drselectAula["codigoGrupo"].ToString() != "")
                                {
                                    lstgrupo.SelectedValue = drselectAula["codigoGrupo"].ToString();
                                }
                                if (drselectAula["codigoDisciplina"].ToString() != "")
                                {
                                    lstmateria.SelectedValue = drselectAula["codigoDisciplina"].ToString();
                                }
                                if (drselectAula["codigoAluno"].ToString() != "")
                                {
                                    txt1.Text = drselectAula["aluno"].ToString();
                                    hide1.Value = drselectAula["codigoAluno"].ToString();
                                }
                             
                                //txtValor1.Text = drselectAula["valorHora"].ToString();
                            }
                            break;
                        case "2":
                            if (inicial2Hora != "" && final2Hora != "")
                            {
                                inicial2Hora2.Text = inicial2Hora;
                                final2Hora2.Text = final2Hora;
                                if (drselectAula["codigoGrupo"].ToString() != "")
                                {
                                    lstgrupo2.SelectedValue = drselectAula["codigoGrupo"].ToString();
                                }
                                if (drselectAula["codigoDisciplina"].ToString() != "")
                                {
                                    lstmateria2.SelectedValue = drselectAula["codigoDisciplina"].ToString();
                                }
                                if (drselectAula["codigoAluno"].ToString() != "")
                                {
                                    txt2.Text = drselectAula["aluno"].ToString();
                                    hide2.Value = drselectAula["codigoAluno"].ToString();
                                }

                                //txtValor2.Text = drselectAula["valorHora"].ToString();
                            }
                            break;
                        case "3":
                            if (inicial3Hora != "" && final3Hora != "")
                            {
                                inicial3Hora3.Text = inicial3Hora;
                                final3Hora3.Text = final3Hora;
                                if (drselectAula["codigoGrupo"].ToString() != "")
                                {
                                    lstgrupo3.SelectedValue = drselectAula["codigoGrupo"].ToString();
                                }
                                if (drselectAula["codigoDisciplina"].ToString() != "")
                                {
                                    lstmateria3.SelectedValue = drselectAula["codigoDisciplina"].ToString();
                                }
                                if (drselectAula["codigoAluno"].ToString() != "")
                                {
                                    txt3.Text = drselectAula["aluno"].ToString();
                                    hide3.Value = drselectAula["codigoAluno"].ToString();
                                }

                                //txtValor3.Text = drselectAula["valorHora"].ToString();
                            }
                            break;
                        case "4":
                            if (inicial4Hora != "" && final4Hora != "")
                            {
                                inicial4Hora4.Text = inicial4Hora;
                                final4Hora4.Text = final4Hora;
                                if (drselectAula["codigoGrupo"].ToString() != "")
                                {
                                    lstgrupo4.SelectedValue = drselectAula["codigoGrupo"].ToString();
                                }
                                if (drselectAula["codigoDisciplina"].ToString() != "")
                                {
                                    lstmateria4.SelectedValue = drselectAula["codigoDisciplina"].ToString();
                                }
                                if (drselectAula["codigoAluno"].ToString() != "")
                                {
                                    txt4.Text = drselectAula["aluno"].ToString();
                                    hide4.Value = drselectAula["codigoAluno"].ToString();
                                }

                                //txtValor4.Text = drselectAula["valorHora"].ToString();
                            }
                            break;
                        case "5":
                            if (inicial5Hora != "" && final5Hora != "")
                            {
                                inicial5Hora5.Text = inicial5Hora;
                                final5Hora5.Text = final5Hora;
                                if (drselectAula["codigoGrupo"].ToString() != "")
                                {
                                    lstgrupo5.SelectedValue = drselectAula["codigoGrupo"].ToString();
                                }
                                if (drselectAula["codigoDisciplina"].ToString() != "")
                                {
                                    lstmateria5.SelectedValue = drselectAula["codigoDisciplina"].ToString();
                                }
                                if (drselectAula["codigoAluno"].ToString() != "")
                                {
                                    txt5.Text = drselectAula["aluno"].ToString();
                                    hide5.Value = drselectAula["codigoAluno"].ToString();
                                }

                                //txtValor5.Text = drselectAula["valorHora"].ToString();
                            }
                            break;
                        case "6":
                            if (inicial6Hora != "" && final6Hora != "")
                            {
                                inicial6Hora6.Text = inicial6Hora;
                                final6Hora6.Text = final6Hora;
                                if (drselectAula["codigoGrupo"].ToString() != "")
                                {
                                    lstgrupo6.SelectedValue = drselectAula["codigoGrupo"].ToString();
                                }
                                if (drselectAula["codigoDisciplina"].ToString() != "")
                                {
                                    lstmateria6.SelectedValue = drselectAula["codigoDisciplina"].ToString();
                                }
                                if (drselectAula["codigoAluno"].ToString() != "")
                                {
                                    txt6.Text = drselectAula["aluno"].ToString();
                                    hide6.Value = drselectAula["codigoAluno"].ToString();
                                }

                                //txtValor6.Text = drselectAula["valorHora"].ToString();
                            }
                            break;
                        case "7":
                            if (inicial7Hora != "" && final7Hora != "")
                            {
                                inicial7Hora7.Text = inicial7Hora;
                                final7Hora7.Text = final7Hora;
                                if (drselectAula["codigoGrupo"].ToString() != "")
                                {
                                    lstgrupo7.SelectedValue = drselectAula["codigoGrupo"].ToString();
                                }
                                if (drselectAula["codigoDisciplina"].ToString() != "")
                                {
                                    lstmateria7.SelectedValue = drselectAula["codigoDisciplina"].ToString();
                                }
                                if (drselectAula["codigoAluno"].ToString() != "")
                                {
                                    txt7.Text = drselectAula["aluno"].ToString();
                                    hide7.Value = drselectAula["codigoAluno"].ToString();
                                }

                                //txtValor7.Text = drselectAula["valorHora"].ToString();
                            }
                            break;
                        default:
                            if (inicial8Hora != "" && final8Hora != "")
                            {
                                inicial8Hora8.Text = inicial8Hora;
                                final8Hora8.Text = final8Hora;
                                if (drselectAula["codigoGrupo"].ToString() != "")
                                {
                                    lstgrupo8.SelectedValue = drselectAula["codigoGrupo"].ToString();
                                }
                                if (drselectAula["codigoDisciplina"].ToString() != "")
                                {
                                    lstmateria8.SelectedValue = drselectAula["codigoDisciplina"].ToString();
                                }
                                if (drselectAula["codigoAluno"].ToString() != "")
                                {
                                    txt8.Text = drselectAula["aluno"].ToString();
                                    hide8.Value = drselectAula["codigoAluno"].ToString();
                                }

                                //txtValor8.Text = drselectAula["valorHora"].ToString();
                            }
                            break;
                    }
                }
            }

            drselectAula.Close();

            sqlj.Close();

            sqlc.Close();

            Panel1.Visible = true;
            BtnSalvar.Visible = true;
        }

        protected void btnSalvar_Click(object sender, EventArgs e)
        {
            string hora1Inicial = "";
            string hora1Final = "";
            string hora2Inicial = "";
            string hora2Final = "";
            string hora3Inicial = "";
            string hora3Final = "";
            string hora4Inicial = "";
            string hora4Final = "";
            string hora5Inicial = "";
            string hora5Final = "";
            string hora6Inicial = "";
            string hora6Final = "";
            string hora7Inicial = "";
            string hora7Final = "";
            string hora8Inicial = "";
            string hora8Final = "";
            bool mensagem = true;

            string dia = lstDia.SelectedValue;
            if (dia.Length == 1)
            {
                dia = "0" + lstDia.SelectedValue;
            }
            string mes = lstMes.SelectedValue;
            if (mes.Length == 1)
            {
                mes = "0" + lstMes.SelectedValue;
            }
            string ano = lstAnoLetivo.SelectedValue;
            if (ano.Length == 1)
            {
                ano = "0" + lstAnoLetivo.SelectedValue;
            }

            string selectVerificar = "SELECT codigoAula, codigoProfessor, codigoAluno, codigoGrupo, dataAula, horaInicialAula, ";
            selectVerificar += "horaFinalAula, codigoDisciplina, descricaoAssuntoAula, observacoesAula,  ";
            selectVerificar += "aulaRealizada, valorHora, valorTotalSemDesconto, valorTotalComDesconto, falta, codigoAnoLetivo, linha, meiaHora, ";
            selectVerificar += "(SELECT MIN(horaInicialAula) AS horaInicial FROM aula AS a WHERE (linha = '1') and (dataAula = '" + dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem + "') AND (codigoProfessor = " + lstProfessor.SelectedValue + ")) AS horaInicial1, ";
            selectVerificar += "(SELECT MAX(horaFinalAula) AS horaFinal FROM aula AS aa WHERE (linha = '1')AND (dataAula = '" + dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem + "') AND (codigoProfessor = " + lstProfessor.SelectedValue + ")) AS horaFinal1, ";
            selectVerificar += "(SELECT MIN(horaInicialAula) AS horaInicial2 FROM aula AS a WHERE (linha = '2') and (dataAula = '" + dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem + "') AND (codigoProfessor = " + lstProfessor.SelectedValue + ")) AS horaInicial2, ";
            selectVerificar += "(SELECT MAX(horaFinalAula) AS horaFinal2 FROM aula AS aa WHERE (linha = '2') and (dataAula = '" + dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem + "') AND (codigoProfessor = " + lstProfessor.SelectedValue + ")) AS horaFinal2, ";
            selectVerificar += "(SELECT MIN(horaInicialAula) AS horaInicial3 FROM aula AS a WHERE (linha = '3') and (dataAula = '" + dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem + "') AND (codigoProfessor = " + lstProfessor.SelectedValue + ")) AS horaInicial3, ";
            selectVerificar += "(SELECT MAX(horaFinalAula) AS horaFinal3 FROM aula AS aa WHERE (linha = '3') and (dataAula = '" + dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem + "') AND (codigoProfessor = " + lstProfessor.SelectedValue + ")) AS horaFinal3, ";
            selectVerificar += "(SELECT MIN(horaInicialAula) AS horaInicial4 FROM aula AS a WHERE (linha = '4') and (dataAula = '" + dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem + "') AND (codigoProfessor = " + lstProfessor.SelectedValue + ")) AS horaInicial4, ";
            selectVerificar += "(SELECT MAX(horaFinalAula) AS horaFinal4 FROM aula AS aa WHERE (linha = '4') and (dataAula = '" + dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem + "') AND (codigoProfessor = " + lstProfessor.SelectedValue + ")) AS horaFinal4, ";
            selectVerificar += "(SELECT MIN(horaInicialAula) AS horaInicial5 FROM aula AS a WHERE (linha = '5') and (dataAula = '" + dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem + "') AND (codigoProfessor = " + lstProfessor.SelectedValue + ")) AS horaInicial5, ";
            selectVerificar += "(SELECT MAX(horaFinalAula) AS horaFinal5 FROM aula AS aa WHERE (linha = '5') and (dataAula = '" + dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem + "') AND (codigoProfessor = " + lstProfessor.SelectedValue + ")) AS horaFinal5, ";
            selectVerificar += "(SELECT MIN(horaInicialAula) AS horaInicial6 FROM aula AS a WHERE (linha = '6') and (dataAula = '" + dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem + "') AND (codigoProfessor = " + lstProfessor.SelectedValue + ")) AS horaInicial6, ";
            selectVerificar += "(SELECT MAX(horaFinalAula) AS horaFinal6 FROM aula AS aa WHERE (linha = '6') and (dataAula = '" + dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem + "') AND (codigoProfessor = " + lstProfessor.SelectedValue + ")) AS horaFinal6, ";
            selectVerificar += "(SELECT MIN(horaInicialAula) AS horaInicial7 FROM aula AS a WHERE (linha = '7') and (dataAula = '" + dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem + "') AND (codigoProfessor = " + lstProfessor.SelectedValue + ")) AS horaInicial7, ";
            selectVerificar += "(SELECT MAX(horaFinalAula) AS horaFinal7 FROM aula AS aa WHERE (linha = '7') and (dataAula = '" + dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem + "') AND (codigoProfessor = " + lstProfessor.SelectedValue + ")) AS horaFinal7, ";
            selectVerificar += "(SELECT MIN(horaInicialAula) AS horaInicial8 FROM aula AS a WHERE (linha = '8') and (dataAula = '" + dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem + "') AND (codigoProfessor = " + lstProfessor.SelectedValue + ")) AS horaInicial8, ";
            selectVerificar += "(SELECT MAX(horaFinalAula) AS horaFinal8 FROM aula AS aa WHERE (linha = '8') and (dataAula = '" + dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem + "') AND (codigoProfessor = " + lstProfessor.SelectedValue + ")) AS horaFinal8 ";
            selectVerificar += "FROM aula WHERE (dataAula = '" + dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem + "') AND (codigoProfessor = " + lstProfessor.SelectedValue + ") ";

            linha1(selectVerificar, dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem, ano, lstProfessor.SelectedValue, hora1Inicial, hora1Final, hora2Inicial, hora2Final, hora3Inicial, hora3Final, hora4Inicial, hora4Final, hora5Inicial, hora5Final, hora6Inicial, hora6Final, hora7Inicial, hora7Final, hora8Inicial, hora8Final, mensagem);
            linha2(selectVerificar, dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem, ano, lstProfessor.SelectedValue, hora1Inicial, hora1Final, hora2Inicial, hora2Final, hora3Inicial, hora3Final, hora4Inicial, hora4Final, hora5Inicial, hora5Final, hora6Inicial, hora6Final, hora7Inicial, hora7Final, hora8Inicial, hora8Final, mensagem);
            linha3(selectVerificar, dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem, ano, lstProfessor.SelectedValue, hora1Inicial, hora1Final, hora2Inicial, hora2Final, hora3Inicial, hora3Final, hora4Inicial, hora4Final, hora5Inicial, hora5Final, hora6Inicial, hora6Final, hora7Inicial, hora7Final, hora8Inicial, hora8Final, mensagem);
            linha4(selectVerificar, dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem, ano, lstProfessor.SelectedValue, hora1Inicial, hora1Final, hora2Inicial, hora2Final, hora3Inicial, hora3Final, hora4Inicial, hora4Final, hora5Inicial, hora5Final, hora6Inicial, hora6Final, hora7Inicial, hora7Final, hora8Inicial, hora8Final, mensagem);
            linha5(selectVerificar, dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem, ano, lstProfessor.SelectedValue, hora1Inicial, hora1Final, hora2Inicial, hora2Final, hora3Inicial, hora3Final, hora4Inicial, hora4Final, hora5Inicial, hora5Final, hora6Inicial, hora6Final, hora7Inicial, hora7Final, hora8Inicial, hora8Final, mensagem);
            linha6(selectVerificar, dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem, ano, lstProfessor.SelectedValue, hora1Inicial, hora1Final, hora2Inicial, hora2Final, hora3Inicial, hora3Final, hora4Inicial, hora4Final, hora5Inicial, hora5Final, hora6Inicial, hora6Final, hora7Inicial, hora7Final, hora8Inicial, hora8Final, mensagem);
            linha7(selectVerificar, dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem, ano, lstProfessor.SelectedValue, hora1Inicial, hora1Final, hora2Inicial, hora2Final, hora3Inicial, hora3Final, hora4Inicial, hora4Final, hora5Inicial, hora5Final, hora6Inicial, hora6Final, hora7Inicial, hora7Final, hora8Inicial, hora8Final, mensagem);
            linha8(selectVerificar, dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem, ano, lstProfessor.SelectedValue, hora1Inicial, hora1Final, hora2Inicial, hora2Final, hora3Inicial, hora3Final, hora4Inicial, hora4Final, hora5Inicial, hora5Final, hora6Inicial, hora6Final, hora7Inicial, hora7Final, hora8Inicial, hora8Final, mensagem);
        }

        void linha1(string selectVerificar, string data, string ano, string professor, string hora1Inicial, string hora1Final, string hora2Inicial, string hora2Final, string hora3Inicial, string hora3Final, string hora4Inicial, string hora4Final, string hora5Inicial, string hora5Final, string hora6Inicial, string hora6Final, string hora7Inicial, string hora7Final, string hora8Inicial, string hora8Final, bool mensagem)
        {


            SqlConnection sqlc = new SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
            sqlc.Open();
            SqlConnection sqld = new SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
            sqld.Open();

            //INSERT E VALIDAÇÕES REFERENTES A 1ª LINHA DE AGENDA DE AULA
            if (hide1.Value != "0" || lstgrupo.SelectedValue != "0")
            {
                if (hide1.Value != "0" && lstgrupo.SelectedValue != "0")
                {
                    //validação selecione um grupo ou um aluno
                    hide1.Value = "0";
                    lstgrupo.SelectedValue = "0";
                    lstmateria.SelectedValue = "0";
                    //mensagem java script
                    string alunoGrupo = "<script language=JavaScript>alunoGrupo1();</script>";
                    Panel2.Controls.Add(new LiteralControl(alunoGrupo));
                }
                else
                {



                    string horaInicial = inicial1Hora1.Text;
                    string horaFinal = final1Hora1.Text;







                    if ((horaInicial.Length == 5 && horaFinal.Length == 5))
                    {




                        if (horaInicial.Length == 4)
                        {
                            horaInicial = "0" + horaInicial;
                        }
                        if (horaFinal.Length == 4)
                        {
                            horaFinal = "0" + horaFinal;
                        }


                       


                        TimeSpan dif = Convert.ToDateTime(horaFinal).Subtract(Convert.ToDateTime(horaInicial));

                        //if (dif.Hours <=0)
                        //{
                        //    string horasIguais = "<script language=JavaScript>horasIguais1();</script>";
                        //    Panel2.Controls.Add(new LiteralControl(horasIguais))
                        //}

                        if (dif.Hours <= 0)
                        {
                            //validação horas iguais

                            //mensagem java script
                            string horasIguais = "<script language=JavaScript>horasIguais1();</script>";
                            Panel2.Controls.Add(new LiteralControl(horasIguais));
                        }
                        else
                        {
                            bool validarInsert = true;

                            SqlCommand cmdVerificar = new SqlCommand(selectVerificar + " and linha <> '1'", sqlc);
                            SqlDataReader drVerificar = cmdVerificar.ExecuteReader();

                            if (drVerificar.HasRows)
                            {
                                while (drVerificar.Read())
                                {
                                    hora1Inicial = drVerificar["horaInicial1"].ToString();
                                    hora1Final = drVerificar["horaFinal1"].ToString();

                                    hora2Inicial = drVerificar["horaInicial2"].ToString();
                                    hora2Final = drVerificar["horaFinal2"].ToString();

                                    hora3Inicial = drVerificar["horaInicial3"].ToString();
                                    hora3Final = drVerificar["horaFinal3"].ToString();

                                    hora4Inicial = drVerificar["horaInicial4"].ToString();
                                    hora4Final = drVerificar["horaFinal4"].ToString();

                                    hora5Inicial = drVerificar["horaInicial5"].ToString();
                                    hora5Final = drVerificar["horaFinal5"].ToString();

                                    hora6Inicial = drVerificar["horaInicial6"].ToString();
                                    hora6Final = drVerificar["horaFinal6"].ToString();

                                    hora7Inicial = drVerificar["horaInicial7"].ToString();
                                    hora7Final = drVerificar["horaFinal7"].ToString();

                                    hora8Inicial = drVerificar["horaInicial8"].ToString();
                                    hora8Final = drVerificar["horaFinal8"].ToString();


                                    switch (drVerificar["linha"].ToString())
                                    {
                                        case "2":
                                            if (hora2Inicial != "" && hora2Final != "")
                                            {
                                                if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial2"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal2"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial2"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal2"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial2"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal2"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }
                                            }
                                            break;
                                        case "3":
                                            if (hora3Inicial != "" && hora3Final != "")
                                            {
                                                if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial3"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal3"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial3"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal3"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial3"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal3"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }
                                            }
                                            break;
                                        case "4":
                                            if (hora4Inicial != "" && hora4Final != "")
                                            {
                                                if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial4"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal4"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial4"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal4"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial4"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal4"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }
                                            }
                                            break;

                                        case "5":
                                            if (hora5Inicial != "" && hora5Final != "")
                                            {
                                                if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial5"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal5"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial5"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal5"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial5"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal5"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }
                                            }
                                            break;

                                        case "6":
                                            if (hora6Inicial != "" && hora6Final != "")
                                            {
                                                if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial6"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal6"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial6"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal6"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial6"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal6"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }
                                            }
                                            break;

                                        case "7":
                                            if (hora7Inicial != "" && hora7Final != "")
                                            {
                                                if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial7"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal7"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial7"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal7"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial7"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal7"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }
                                            }
                                            break;

                                        case "8":
                                            if (hora8Inicial != "" && hora8Final != "")
                                            {
                                                if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial8"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal8"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial8"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal8"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial8"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal8"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }
                                            }
                                            break;
                                        default:
                                            break;
                                    }
                                }
                            }
                            drVerificar.Close();

                            if (validarInsert)
                            {
                                string sqlInserirAula = "";

                                if (verificarAulaAluno(hide1.Value, lstgrupo.SelectedValue, data, horaInicial, horaFinal, "1", lstProfessor.SelectedValue))
                                {
                                    if (hide1.Value != "0")
                                    {
                                        DateTime hI = Convert.ToDateTime(horaInicial);

                                        string sqlDeletarAula = "delete from aula where linha = '1' and codigoProfessor = " + lstProfessor.SelectedValue + " and dataAula = '" + data + "'";

                                        SqlCommand cmdDelete = new SqlCommand(sqlDeletarAula, sqld);
                                        cmdDelete.ExecuteNonQuery();
                                        if ((lstmateria.SelectedValue != "0"))
                                        {
                                            int i = 0;
                                            while (i < dif.TotalHours)
                                            {
                                                DateTime hF = hI;
                                                hF += new TimeSpan(0, 0, 3600);

                                                string horaI = hI.ToString("HH:mm");
                                                string horaF = hF.ToString("HH:mm");

                                                if (hI.ToString().Length == 4)
                                                {
                                                    horaI = "0" + hI.ToString("HH:mm");
                                                }
                                                if (hF.ToString().Length == 4)
                                                {
                                                    horaF = "0" + hF.ToString("HH:mm");
                                                }

                                                sqlInserirAula = "INSERT INTO aula ";
                                                sqlInserirAula = sqlInserirAula + "(codigoDisciplina, codigoProfessor, horaInicialAula, horaFinalAula, dataAula, codigoAluno, codigoAnoLetivo, linha, meiaHora, codigoTipoAula)";
                                                sqlInserirAula = sqlInserirAula + " VALUES (" + lstmateria.SelectedValue + ", " + lstProfessor.SelectedValue + ", ";
                                                sqlInserirAula = sqlInserirAula + " '" + horaI + "', '" + horaF + "', '" + data + "', " + hide1.Value + ", " + ano + ", '1', 0, 1)";

                                                if (Convert.ToDateTime(horaFinal) != hF)
                                                {
                                                    hI = hF;
                                                }

                                                SqlCommand cmdInsert = new SqlCommand(sqlInserirAula, sqld);
                                                cmdInsert.ExecuteNonQuery();

                                                i++;
                                                if ((Convert.ToDouble(dif.TotalHours) - i) == 0.5)
                                                {
                                                    hF = hI;
                                                    hF += new TimeSpan(0, 0, 1800);

                                                    horaI = hI.ToString("HH:mm");
                                                    horaF = hF.ToString("HH:mm");

                                                    if (hI.ToString().Length == 4)
                                                    {
                                                        horaI = "0" + hI.ToString("HH:mm");
                                                    }
                                                    if (hF.ToString().Length == 4)
                                                    {
                                                        horaF = "0" + hF.ToString("HH:mm");
                                                    }

                                                    sqlInserirAula = "INSERT INTO aula ";
                                                    sqlInserirAula = sqlInserirAula + "(codigoDisciplina, codigoProfessor, horaInicialAula, horaFinalAula, dataAula, codigoAluno, codigoAnoLetivo, linha, meiaHora, codigoTipoAula)";
                                                    sqlInserirAula = sqlInserirAula + " VALUES (" + lstmateria.SelectedValue + ", " + lstProfessor.SelectedValue + ", ";
                                                    sqlInserirAula = sqlInserirAula + " '" + horaI + "', '" + horaF + "', '" + data + "', " + hide1.Value + ", " + ano + ", '1', 1, 1)";

                                                    cmdInsert = new SqlCommand(sqlInserirAula, sqld);
                                                    cmdInsert.ExecuteNonQuery();
                                                    break;
                                                }
                                            }
                                        }

                                        //mensagem java script
                                        string finalizar = "<script language=JavaScript>finalizar1();</script>";
                                        Panel2.Controls.Add(new LiteralControl(finalizar));

                                        mensagem = false;
                                    }
                                    else
                                    {
                                        DateTime hI = Convert.ToDateTime(horaInicial);

                                        string sqlDeletarAula = "delete from aula where linha = '1' and codigoProfessor = " + lstProfessor.SelectedValue + " and dataAula = '" + data + "'";

                                        SqlCommand cmdDelete = new SqlCommand(sqlDeletarAula, sqld);
                                        cmdDelete.ExecuteNonQuery();

                                        if ((lstmateria.SelectedValue != "0"))
                                        {
                                            int i = 0;
                                            while (i < dif.TotalHours)
                                            {
                                                DateTime hF = hI;
                                                hF += new TimeSpan(0, 0, 3600);

                                                string horaI = hI.ToString("HH:mm");
                                                string horaF = hF.ToString("HH:mm");

                                                if (hI.ToString().Length == 4)
                                                {
                                                    horaI = "0" + hI.ToString("HH:mm");
                                                }
                                                if (hF.ToString().Length == 4)
                                                {
                                                    horaF = "0" + hF.ToString("HH:mm");
                                                }
                                                sqlInserirAula = "INSERT INTO aula ";
                                                sqlInserirAula = sqlInserirAula + "(codigoDisciplina, codigoProfessor, horaInicialAula, horaFinalAula, dataAula, codigoAnoLetivo, codigoGrupo, linha, meiaHora, codigoTipoAula)";
                                                sqlInserirAula = sqlInserirAula + " VALUES (" + lstmateria.SelectedValue + ", " + lstProfessor.SelectedValue + ", ";
                                                sqlInserirAula = sqlInserirAula + " '" + horaI + "', '" + horaF + "', '" + data + "', " + ano + ", " + lstgrupo.SelectedValue + ", '1', 0, 2)";

                                                if (Convert.ToDateTime(horaFinal) != hF)
                                                {
                                                    hI = hF;
                                                }

                                                SqlCommand cmdInsert = new SqlCommand(sqlInserirAula, sqld);
                                                cmdInsert.ExecuteNonQuery();

                                                i++;
                                                if ((Convert.ToDouble(dif.TotalHours) - i) == 0.5)
                                                {
                                                    hF = hI;
                                                    hF += new TimeSpan(0, 0, 1800);

                                                    horaI = hI.ToString("HH:mm");
                                                    horaF = hF.ToString("HH:mm");

                                                    if (hI.ToString().Length == 4)
                                                    {
                                                        horaI = "0" + hI.ToString("HH:mm");
                                                    }
                                                    if (hF.ToString().Length == 4)
                                                    {
                                                        horaF = "0" + hF.ToString("HH:mm");
                                                    }

                                                    sqlInserirAula = "INSERT INTO aula ";
                                                    sqlInserirAula = sqlInserirAula + "(codigoDisciplina, codigoProfessor, horaInicialAula, horaFinalAula, dataAula, codigoAnoLetivo, codigoGrupo, linha, meiaHora, codigoTipoAula)";
                                                    sqlInserirAula = sqlInserirAula + " VALUES (" + lstmateria.SelectedValue + ", " + lstProfessor.SelectedValue + ", ";
                                                    sqlInserirAula = sqlInserirAula + " '" + horaI + "', '" + horaF + "', '" + data + "', " + ano + ", " + lstgrupo.SelectedValue + ", '1', 1, 2)";

                                                    cmdInsert = new SqlCommand(sqlInserirAula, sqld);
                                                    cmdInsert.ExecuteNonQuery();


                                                    //mensagem java script
                                                    string finalizar = "<script language=JavaScript>finalizar1();</script>";
                                                    Panel2.Controls.Add(new LiteralControl(finalizar));

                                                    break;
                                                }
                                            }
                                        }



                                        mensagem = false;
                                    }
                                }
                            }
                            else
                            {
                                //validar já existe aula nessas horas

                                //mensagem java script
                                string existe = "<script language=JavaScript>existe1();</script>";
                                Panel2.Controls.Add(new LiteralControl(existe));
                            }
                        }








                    }
                    else {





                        if ((horaInicial != "" && horaFinal != ""))
                        {



                            //mensagem java script
                            string existe = "<script language=JavaScript>alert('As horas inicial e final devem estar no formato hh:mm.');</script>";
                            Panel2.Controls.Add(new LiteralControl(existe));

                            if (horaFinal.Length != 5)
                            {

                                final1Hora1.Focus();

                            }


                            if (horaInicial.Length != 5)
                            {

                                inicial1Hora1.Focus();

                            }

                        }
                    
                    
                    }
                    









                }
            }
            else
            {
                string selectDeletar = "select * from aula where dataAula = '" + data + "' and codigoProfessor = " + lstProfessor.SelectedValue + " and linha = 1 and aulaRealizada <> 1";

                SqlCommand cmd = new SqlCommand(selectDeletar, sqlc);
                SqlDataReader dr = cmd.ExecuteReader();

                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        string delete = "delete from aula where codigoAula = " + dr["codigoAula"].ToString();

                        SqlCommand cmdDeletar = new SqlCommand(delete, sqld);
                        cmdDeletar.ExecuteNonQuery();
                    }
                }


            }

            sqlc.Close();
            sqld.Close();
        }

        void linha2(string selectVerificar, string data, string ano, string professor, string hora1Inicial, string hora1Final, string hora2Inicial, string hora2Final, string hora3Inicial, string hora3Final, string hora4Inicial, string hora4Final, string hora5Inicial, string hora5Final, string hora6Inicial, string hora6Final, string hora7Inicial, string hora7Final, string hora8Inicial, string hora8Final, bool mensagem)
        {
            SqlConnection sqlc = new SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
            sqlc.Open();
            SqlConnection sqld = new SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
            sqld.Open();

            //INSERT E VALIDAÇÕES REFERENTES A 1ª LINHA DE AGENDA DE AULA
            if (hide2.Value != "0" || lstgrupo2.SelectedValue != "0")
            {
                if (hide2.Value != "0" && lstgrupo2.SelectedValue != "0")
                {
                    //validação selecione um grupo ou um aluno
                    hide2.Value = "0";
                    lstgrupo2.SelectedValue = "0";
                    lstmateria2.SelectedValue = "0";
                    //mensagem java script
                    string alunoGrupo = "<script language=JavaScript>alunoGrupo2();</script>";
                    Panel2.Controls.Add(new LiteralControl(alunoGrupo));
                }
                else
                {
                    string horaInicial = inicial2Hora2.Text;
                    string horaFinal = final2Hora2.Text;



                    if ((horaInicial.Length == 5 && horaFinal.Length == 5))
                    {










                        if (horaInicial.Length == 4)
                        {
                            horaInicial = "0" + horaInicial;
                        }
                        if (horaFinal.Length == 4)
                        {
                            horaFinal = "0" + horaFinal;
                        }

                        TimeSpan dif = Convert.ToDateTime(horaFinal).Subtract(Convert.ToDateTime(horaInicial));

                        if (dif.Hours <= 0)
                        {
                            //validação horas iguais

                            //mensagem java script
                            string horasIguais = "<script language=JavaScript>horasIguais2();</script>";
                            Panel2.Controls.Add(new LiteralControl(horasIguais));
                        }
                        else
                        {
                            bool validarInsert = true;

                            SqlCommand cmdVerificar = new SqlCommand(selectVerificar + " and linha <> '2'", sqlc);
                            SqlDataReader drVerificar = cmdVerificar.ExecuteReader();

                            if (drVerificar.HasRows)
                            {
                                while (drVerificar.Read())
                                {
                                    hora1Inicial = drVerificar["horaInicial1"].ToString();
                                    hora1Final = drVerificar["horaFinal1"].ToString();

                                    hora2Inicial = drVerificar["horaInicial2"].ToString();
                                    hora2Final = drVerificar["horaFinal2"].ToString();

                                    hora3Inicial = drVerificar["horaInicial3"].ToString();
                                    hora3Final = drVerificar["horaFinal3"].ToString();

                                    hora4Inicial = drVerificar["horaInicial4"].ToString();
                                    hora4Final = drVerificar["horaFinal4"].ToString();

                                    hora5Inicial = drVerificar["horaInicial5"].ToString();
                                    hora5Final = drVerificar["horaFinal5"].ToString();

                                    hora6Inicial = drVerificar["horaInicial6"].ToString();
                                    hora6Final = drVerificar["horaFinal6"].ToString();

                                    hora7Inicial = drVerificar["horaInicial7"].ToString();
                                    hora7Final = drVerificar["horaFinal7"].ToString();

                                    hora8Inicial = drVerificar["horaInicial8"].ToString();
                                    hora8Final = drVerificar["horaFinal8"].ToString();


                                    switch (drVerificar["linha"].ToString())
                                    {
                                        case "1":
                                            if (hora1Inicial != "" && hora1Final != "")
                                            {
                                                if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial1"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal1"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial1"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal1"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial1"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal1"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }
                                            }
                                            break;
                                        case "3":
                                            if (hora3Inicial != "" && hora3Final != "")
                                            {
                                                if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial3"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal3"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial3"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal3"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial3"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal3"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }
                                            }
                                            break;
                                        case "4":
                                            if (hora4Inicial != "" && hora4Final != "")
                                            {
                                                if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial4"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal4"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial4"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal4"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial4"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal4"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }
                                            }
                                            break;

                                        case "5":
                                            if (hora5Inicial != "" && hora5Final != "")
                                            {
                                                if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial5"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal5"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial5"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal5"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial5"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal5"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }
                                            }
                                            break;

                                        case "6":
                                            if (hora6Inicial != "" && hora6Final != "")
                                            {
                                                if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial6"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal6"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial6"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal6"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial6"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal6"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }
                                            }
                                            break;

                                        case "7":
                                            if (hora7Inicial != "" && hora7Final != "")
                                            {
                                                if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial7"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal7"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial7"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal7"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial7"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal7"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }
                                            }
                                            break;

                                        case "8":
                                            if (hora8Inicial != "" && hora8Final != "")
                                            {
                                                if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial8"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal8"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial8"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal8"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial8"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal8"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }
                                            }
                                            break;
                                        default:
                                            break;
                                    }
                                }
                            }
                            drVerificar.Close();

                            if (validarInsert)
                            {
                                string sqlInserirAula = "";
                                if (verificarAulaAluno(hide2.Value, lstgrupo2.SelectedValue, data, horaInicial, horaFinal, "2", lstProfessor.SelectedValue))
                                {
                                    if (hide2.Value != "0")
                                    {
                                        DateTime hI = Convert.ToDateTime(horaInicial);

                                        string sqlDeletarAula = "delete from aula where linha = '2' and codigoProfessor = " + lstProfessor.SelectedValue + " and dataAula = '" + data + "'";

                                        SqlCommand cmdDelete = new SqlCommand(sqlDeletarAula, sqld);
                                        cmdDelete.ExecuteNonQuery();
                                        if ((lstmateria2.SelectedValue != "0"))
                                        {
                                            int i = 0;
                                            while (i < dif.TotalHours)
                                            {
                                                DateTime hF = hI;
                                                hF += new TimeSpan(0, 0, 3600);

                                                string horaI = hI.ToString("HH:mm");
                                                string horaF = hF.ToString("HH:mm");

                                                if (hI.ToString().Length == 4)
                                                {
                                                    horaI = "0" + hI.ToString("HH:mm");
                                                }
                                                if (hF.ToString().Length == 4)
                                                {
                                                    horaF = "0" + hF.ToString("HH:mm");
                                                }

                                                sqlInserirAula = "INSERT INTO aula ";
                                                sqlInserirAula = sqlInserirAula + "(codigoDisciplina, codigoProfessor, horaInicialAula, horaFinalAula, dataAula, codigoAluno, codigoAnoLetivo, linha, meiaHora, codigoTipoAula)";
                                                sqlInserirAula = sqlInserirAula + " VALUES (" + lstmateria2.SelectedValue + ", " + lstProfessor.SelectedValue + ", ";
                                                sqlInserirAula = sqlInserirAula + " '" + horaI + "', '" + horaF + "', '" + data + "', " + hide2.Value + ", " + ano + ", '2', 0, 1)";

                                                if (Convert.ToDateTime(horaFinal) != hF)
                                                {
                                                    hI = hF;
                                                }

                                                SqlCommand cmdInsert = new SqlCommand(sqlInserirAula, sqld);
                                                cmdInsert.ExecuteNonQuery();

                                                i++;
                                                if ((Convert.ToDouble(dif.TotalHours) - i) == 0.5)
                                                {
                                                    hF = hI;
                                                    hF += new TimeSpan(0, 0, 1800);

                                                    horaI = hI.ToString("HH:mm");
                                                    horaF = hF.ToString("HH:mm");

                                                    if (hI.ToString().Length == 4)
                                                    {
                                                        horaI = "0" + hI.ToString("HH:mm");
                                                    }
                                                    if (hF.ToString().Length == 4)
                                                    {
                                                        horaF = "0" + hF.ToString("HH:mm");
                                                    }

                                                    sqlInserirAula = "INSERT INTO aula ";
                                                    sqlInserirAula = sqlInserirAula + "(codigoDisciplina, codigoProfessor, horaInicialAula, horaFinalAula, dataAula, codigoAluno, codigoAnoLetivo, linha, meiaHora, codigoTipoAula)";
                                                    sqlInserirAula = sqlInserirAula + " VALUES (" + lstmateria2.SelectedValue + ", " + lstProfessor.SelectedValue + ", ";
                                                    sqlInserirAula = sqlInserirAula + " '" + horaI + "', '" + horaF + "', '" + data + "', " + hide2.Value + ", " + ano + ", '2', 1, 1)";

                                                    cmdInsert = new SqlCommand(sqlInserirAula, sqld);
                                                    cmdInsert.ExecuteNonQuery();
                                                    break;
                                                }
                                            }
                                        }

                                        //mensagem java script
                                        string finalizar = "<script language=JavaScript>finalizar2();</script>";
                                        Panel2.Controls.Add(new LiteralControl(finalizar));

                                        mensagem = false;
                                    }
                                    else
                                    {
                                        DateTime hI = Convert.ToDateTime(horaInicial);

                                        string sqlDeletarAula = "delete from aula where linha = '2' and codigoProfessor = " + lstProfessor.SelectedValue + " and dataAula = '" + data + "'";

                                        SqlCommand cmdDelete = new SqlCommand(sqlDeletarAula, sqld);
                                        cmdDelete.ExecuteNonQuery();

                                        if ((lstmateria2.SelectedValue != "0"))
                                        {
                                            int i = 0;
                                            while (i < dif.TotalHours)
                                            {
                                                DateTime hF = hI;
                                                hF += new TimeSpan(0, 0, 3600);

                                                string horaI = hI.ToString("HH:mm");
                                                string horaF = hF.ToString("HH:mm");

                                                if (hI.ToString().Length == 4)
                                                {
                                                    horaI = "0" + hI.ToString("HH:mm");
                                                }
                                                if (hF.ToString().Length == 4)
                                                {
                                                    horaF = "0" + hF.ToString("HH:mm");
                                                }
                                                sqlInserirAula = "INSERT INTO aula ";
                                                sqlInserirAula = sqlInserirAula + "(codigoDisciplina, codigoProfessor, horaInicialAula, horaFinalAula, dataAula, codigoAnoLetivo, codigoGrupo, linha, meiaHora, codigoTipoAula)";
                                                sqlInserirAula = sqlInserirAula + " VALUES (" + lstmateria2.SelectedValue + ", " + lstProfessor.SelectedValue + ", ";
                                                sqlInserirAula = sqlInserirAula + " '" + horaI + "', '" + horaF + "', '" + data + "', " + ano + ", " + lstgrupo2.SelectedValue + ",'2', 0, 2)";

                                                if (Convert.ToDateTime(horaFinal) != hF)
                                                {
                                                    hI = hF;
                                                }

                                                SqlCommand cmdInsert = new SqlCommand(sqlInserirAula, sqld);
                                                cmdInsert.ExecuteNonQuery();

                                                i++;
                                                if ((Convert.ToDouble(dif.TotalHours) - i) == 0.5)
                                                {
                                                    hF = hI;
                                                    hF += new TimeSpan(0, 0, 1800);

                                                    horaI = hI.ToString("HH:mm");
                                                    horaF = hF.ToString("HH:mm");

                                                    if (hI.ToString().Length == 4)
                                                    {
                                                        horaI = "0" + hI.ToString("HH:mm");
                                                    }
                                                    if (hF.ToString().Length == 4)
                                                    {
                                                        horaF = "0" + hF.ToString("HH:mm");
                                                    }

                                                    sqlInserirAula = "INSERT INTO aula ";
                                                    sqlInserirAula = sqlInserirAula + "(codigoDisciplina, codigoProfessor, horaInicialAula, horaFinalAula, dataAula, codigoAnoLetivo, codigoGrupo, linha, meiaHora, codigoTipoAula)";
                                                    sqlInserirAula = sqlInserirAula + " VALUES (" + lstmateria2.SelectedValue + ", " + lstProfessor.SelectedValue + ", ";
                                                    sqlInserirAula = sqlInserirAula + " '" + horaI + "', '" + horaF + "', '" + data + "', " + ano + ", " + lstgrupo2.SelectedValue + ", '2', 1, 2)";

                                                    cmdInsert = new SqlCommand(sqlInserirAula, sqld);
                                                    cmdInsert.ExecuteNonQuery();

                                                    //mensagem java script
                                                    string finalizar = "<script language=JavaScript>finalizar2();</script>";
                                                    Panel2.Controls.Add(new LiteralControl(finalizar));

                                                    break;
                                                }
                                            }
                                        }



                                        mensagem = false;
                                    }
                                }
                            }
                            else
                            {
                                //validar já existe aula nessas horas

                                //mensagem java script
                                string existe = "<script language=JavaScript>existe2();</script>";
                                Panel2.Controls.Add(new LiteralControl(existe));
                            }
                        }
                    }






                    else
                    {

                        if ((horaInicial != "" && horaFinal != ""))
                        {
                            //mensagem java script
                            string existe = "<script language=JavaScript>alert('As horas inicial e final devem estar no formato hh:mm.');</script>";
                            Panel2.Controls.Add(new LiteralControl(existe));

                            if (horaFinal.Length != 5)
                            {

                                final2Hora2.Focus();

                            }


                            if (horaInicial.Length != 5)
                            {

                                inicial2Hora2.Focus();

                            }


                        }
                    }






                    {
                        string selectDeletar = "select * from aula where dataAula = '" + data + "' and codigoProfessor = " + lstProfessor.SelectedValue + " and linha = 2 and aulaRealizada <> 1";

                        SqlCommand cmd = new SqlCommand(selectDeletar, sqlc);
                        SqlDataReader dr = cmd.ExecuteReader();

                        if (dr.HasRows)
                        {
                            while (dr.Read())
                            {
                                string delete = "delete from aula where codigoAula = " + dr["codigoAula"].ToString();

                                SqlCommand cmdDeletar = new SqlCommand(delete, sqld);
                                cmdDeletar.ExecuteNonQuery();
                            }
                        }


                    }

                    sqlc.Close();
                    sqld.Close();
                }
            }
        }

        void linha3(string selectVerificar, string data, string ano, string professor, string hora1Inicial, string hora1Final, string hora2Inicial, string hora2Final, string hora3Inicial, string hora3Final, string hora4Inicial, string hora4Final, string hora5Inicial, string hora5Final, string hora6Inicial, string hora6Final, string hora7Inicial, string hora7Final, string hora8Inicial, string hora8Final, bool mensagem)
        {
            SqlConnection sqlc = new SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
            sqlc.Open();
            SqlConnection sqld = new SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
            sqld.Open();

            //INSERT E VALIDAÇÕES REFERENTES A 1ª LINHA DE AGENDA DE AULA
            if (hide3.Value != "0" || lstgrupo3.SelectedValue != "0")
            {
                if (hide3.Value != "0" && lstgrupo3.SelectedValue != "0")
                {
                    //validação selecione um grupo ou um aluno
                    hide3.Value = "0";
                    lstgrupo3.SelectedValue = "0";
                    lstmateria3.SelectedValue = "0";
                    //mensagem java script
                    string alunoGrupo = "<script language=JavaScript>alunoGrupo3();</script>";
                    Panel2.Controls.Add(new LiteralControl(alunoGrupo));
                }
                else
                {
                    string horaInicial = inicial3Hora3.Text;
                    string horaFinal = final3Hora3.Text;


                    if ((horaInicial.Length == 5 && horaFinal.Length == 5))
                    {









                        if (horaInicial.Length == 4)
                        {
                            horaInicial = "0" + horaInicial;
                        }
                        if (horaFinal.Length == 4)
                        {
                            horaFinal = "0" + horaFinal;
                        }

                        TimeSpan dif = Convert.ToDateTime(horaFinal).Subtract(Convert.ToDateTime(horaInicial));

                        if (dif.Hours <= 0)
                        {
                            //validação horas iguais

                            //mensagem java script
                            string horasIguais = "<script language=JavaScript>horasIguais3();</script>";
                            Panel2.Controls.Add(new LiteralControl(horasIguais));
                        }
                        else
                        {
                            bool validarInsert = true;

                            SqlCommand cmdVerificar = new SqlCommand(selectVerificar + " and linha <> '3'", sqlc);
                            SqlDataReader drVerificar = cmdVerificar.ExecuteReader();

                            if (drVerificar.HasRows)
                            {
                                while (drVerificar.Read())
                                {
                                    hora1Inicial = drVerificar["horaInicial1"].ToString();
                                    hora1Final = drVerificar["horaFinal1"].ToString();

                                    hora2Inicial = drVerificar["horaInicial2"].ToString();
                                    hora2Final = drVerificar["horaFinal2"].ToString();

                                    hora3Inicial = drVerificar["horaInicial3"].ToString();
                                    hora3Final = drVerificar["horaFinal3"].ToString();

                                    hora4Inicial = drVerificar["horaInicial4"].ToString();
                                    hora4Final = drVerificar["horaFinal4"].ToString();

                                    hora5Inicial = drVerificar["horaInicial5"].ToString();
                                    hora5Final = drVerificar["horaFinal5"].ToString();

                                    hora6Inicial = drVerificar["horaInicial6"].ToString();
                                    hora6Final = drVerificar["horaFinal6"].ToString();

                                    hora7Inicial = drVerificar["horaInicial7"].ToString();
                                    hora7Final = drVerificar["horaFinal7"].ToString();

                                    hora8Inicial = drVerificar["horaInicial8"].ToString();
                                    hora8Final = drVerificar["horaFinal8"].ToString();


                                    switch (drVerificar["linha"].ToString())
                                    {
                                        case "2":
                                            if (hora2Inicial != "" && hora2Final != "")
                                            {
                                                if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial2"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal2"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial2"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal2"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial2"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal2"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }
                                            }
                                            break;
                                        case "1":
                                            if (hora1Inicial != "" && hora1Final != "")
                                            {
                                                if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial1"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal1"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial1"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal1"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial1"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal1"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }
                                            }
                                            break;
                                        case "4":
                                            if (hora4Inicial != "" && hora4Final != "")
                                            {
                                                if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial4"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal4"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial4"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal4"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial4"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal4"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }
                                            }
                                            break;

                                        case "5":
                                            if (hora5Inicial != "" && hora5Final != "")
                                            {
                                                if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial5"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal5"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial5"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal5"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial5"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal5"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }
                                            }
                                            break;

                                        case "6":
                                            if (hora6Inicial != "" && hora6Final != "")
                                            {
                                                if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial6"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal6"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial6"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal6"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial6"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal6"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }
                                            }
                                            break;

                                        case "7":
                                            if (hora7Inicial != "" && hora7Final != "")
                                            {
                                                if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial7"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal7"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial7"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal7"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial7"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal7"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }
                                            }
                                            break;

                                        case "8":
                                            if (hora8Inicial != "" && hora8Final != "")
                                            {
                                                if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial8"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal8"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial8"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal8"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial8"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal8"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }
                                            }
                                            break;
                                        default:
                                            break;
                                    }
                                }
                            }
                            drVerificar.Close();

                            if (validarInsert)
                            {
                                string sqlInserirAula = "";
                                if (verificarAulaAluno(hide3.Value, lstgrupo3.SelectedValue, data, horaInicial, horaFinal, "3", lstProfessor.SelectedValue))
                                {
                                    if (hide3.Value != "0")
                                    {
                                        DateTime hI = Convert.ToDateTime(horaInicial);

                                        string sqlDeletarAula = "delete from aula where linha = '3' and codigoProfessor = " + lstProfessor.SelectedValue + " and dataAula = '" + data + "'";

                                        SqlCommand cmdDelete = new SqlCommand(sqlDeletarAula, sqld);
                                        cmdDelete.ExecuteNonQuery();
                                        if ((lstmateria3.SelectedValue != "0"))
                                        {
                                            int i = 0;
                                            while (i < dif.TotalHours)
                                            {
                                                DateTime hF = hI;
                                                hF += new TimeSpan(0, 0, 3600);

                                                string horaI = hI.ToString("HH:mm");
                                                string horaF = hF.ToString("HH:mm");

                                                if (hI.ToString().Length == 4)
                                                {
                                                    horaI = "0" + hI.ToString("HH:mm");
                                                }
                                                if (hF.ToString().Length == 4)
                                                {
                                                    horaF = "0" + hF.ToString("HH:mm");
                                                }

                                                sqlInserirAula = "INSERT INTO aula ";
                                                sqlInserirAula = sqlInserirAula + "(codigoDisciplina, codigoProfessor, horaInicialAula, horaFinalAula, dataAula, codigoAluno, codigoAnoLetivo, linha, meiaHora, codigoTipoAula)";
                                                sqlInserirAula = sqlInserirAula + " VALUES (" + lstmateria3.SelectedValue + ", " + lstProfessor.SelectedValue + ", ";
                                                sqlInserirAula = sqlInserirAula + " '" + horaI + "', '" + horaF + "', '" + data + "', " + hide3.Value + ", " + ano + ", '3', 0, 1)";

                                                if (Convert.ToDateTime(horaFinal) != hF)
                                                {
                                                    hI = hF;
                                                }

                                                SqlCommand cmdInsert = new SqlCommand(sqlInserirAula, sqld);
                                                cmdInsert.ExecuteNonQuery();

                                                i++;
                                                if ((Convert.ToDouble(dif.TotalHours) - i) == 0.5)
                                                {
                                                    hF = hI;
                                                    hF += new TimeSpan(0, 0, 1800);

                                                    horaI = hI.ToString("HH:mm");
                                                    horaF = hF.ToString("HH:mm");

                                                    if (hI.ToString().Length == 4)
                                                    {
                                                        horaI = "0" + hI.ToString("HH:mm");
                                                    }
                                                    if (hF.ToString().Length == 4)
                                                    {
                                                        horaF = "0" + hF.ToString("HH:mm");
                                                    }

                                                    sqlInserirAula = "INSERT INTO aula ";
                                                    sqlInserirAula = sqlInserirAula + "(codigoDisciplina, codigoProfessor, horaInicialAula, horaFinalAula, dataAula, codigoAluno, codigoAnoLetivo, linha, meiaHora, codigoTipoAula)";
                                                    sqlInserirAula = sqlInserirAula + " VALUES (" + lstmateria3.SelectedValue + ", " + lstProfessor.SelectedValue + ", ";
                                                    sqlInserirAula = sqlInserirAula + " '" + horaI + "', '" + horaF + "', '" + data + "', " + hide3.Value + ", " + ano + ", '3', 1, 1)";

                                                    cmdInsert = new SqlCommand(sqlInserirAula, sqld);
                                                    cmdInsert.ExecuteNonQuery();
                                                    break;
                                                }
                                            }
                                        }

                                        //mensagem java script
                                        string finalizar = "<script language=JavaScript>finalizar3();</script>";
                                        Panel2.Controls.Add(new LiteralControl(finalizar));

                                        mensagem = false;
                                    }
                                    else
                                    {
                                        DateTime hI = Convert.ToDateTime(horaInicial);

                                        string sqlDeletarAula = "delete from aula where linha = '3' and codigoProfessor = " + lstProfessor.SelectedValue + " and dataAula = '" + data + "'";

                                        SqlCommand cmdDelete = new SqlCommand(sqlDeletarAula, sqld);
                                        cmdDelete.ExecuteNonQuery();

                                        if ((lstmateria3.SelectedValue != "0"))
                                        {
                                            int i = 0;
                                            while (i < dif.TotalHours)
                                            {
                                                DateTime hF = hI;
                                                hF += new TimeSpan(0, 0, 3600);

                                                string horaI = hI.ToString("HH:mm");
                                                string horaF = hF.ToString("HH:mm");

                                                if (hI.ToString().Length == 4)
                                                {
                                                    horaI = "0" + hI.ToString("HH:mm");
                                                }
                                                if (hF.ToString().Length == 4)
                                                {
                                                    horaF = "0" + hF.ToString("HH:mm");
                                                }
                                                sqlInserirAula = "INSERT INTO aula ";
                                                sqlInserirAula = sqlInserirAula + "(codigoDisciplina, codigoProfessor, horaInicialAula, horaFinalAula, dataAula, codigoAnoLetivo, codigoGrupo, linha, meiaHora, codigoTipoAula)";
                                                sqlInserirAula = sqlInserirAula + " VALUES (" + lstmateria3.SelectedValue + ", " + lstProfessor.SelectedValue + ", ";
                                                sqlInserirAula = sqlInserirAula + " '" + horaI + "', '" + horaF + "', '" + data + "', " + ano + ", " + lstgrupo3.SelectedValue + ", '3', 0, 2)";

                                                if (Convert.ToDateTime(horaFinal) != hF)
                                                {
                                                    hI = hF;
                                                }

                                                SqlCommand cmdInsert = new SqlCommand(sqlInserirAula, sqld);
                                                cmdInsert.ExecuteNonQuery();

                                                i++;
                                                if ((Convert.ToDouble(dif.TotalHours) - i) == 0.5)
                                                {
                                                    hF = hI;
                                                    hF += new TimeSpan(0, 0, 1800);

                                                    horaI = hI.ToString("HH:mm");
                                                    horaF = hF.ToString("HH:mm");

                                                    if (hI.ToString().Length == 4)
                                                    {
                                                        horaI = "0" + hI.ToString("HH:mm");
                                                    }
                                                    if (hF.ToString().Length == 4)
                                                    {
                                                        horaF = "0" + hF.ToString("HH:mm");
                                                    }

                                                    sqlInserirAula = "INSERT INTO aula ";
                                                    sqlInserirAula = sqlInserirAula + "(codigoDisciplina, codigoProfessor, horaInicialAula, horaFinalAula, dataAula, codigoAnoLetivo, codigoGrupo, linha, meiaHora, codigoTipoAula)";
                                                    sqlInserirAula = sqlInserirAula + " VALUES (" + lstmateria3.SelectedValue + ", " + lstProfessor.SelectedValue + ", ";
                                                    sqlInserirAula = sqlInserirAula + " '" + horaI + "', '" + horaF + "', '" + data + "', " + ano + ", " + lstgrupo3.SelectedValue + ", '3', 1, 2)";

                                                    cmdInsert = new SqlCommand(sqlInserirAula, sqld);
                                                    cmdInsert.ExecuteNonQuery();


                                                    //mensagem java script
                                                    string finalizar = "<script language=JavaScript>finalizar3();</script>";
                                                    Panel2.Controls.Add(new LiteralControl(finalizar));

                                                    break;
                                                }
                                            }
                                        }


                                        mensagem = false;
                                    }
                                }
                            }
                            else
                            {
                                //validar já existe aula nessas horas

                                //mensagem java script
                                string existe = "<script language=JavaScript>existe3();</script>";
                                Panel2.Controls.Add(new LiteralControl(existe));
                            }
                        }
                    }






                    else
                    {

                        if ((horaInicial != "" && horaFinal != ""))
                        {
                            //mensagem java script
                            string existe = "<script language=JavaScript>alert('As horas inicial e final devem estar no formato hh:mm.');</script>";
                            Panel2.Controls.Add(new LiteralControl(existe));

                            if (horaFinal.Length != 5)
                            {

                                final3Hora3.Focus();

                            }


                            if (horaInicial.Length != 5)
                            {

                                inicial3Hora3.Focus();

                            }


                        }
                    }








                    {
                        string selectDeletar = "select * from aula where dataAula = '" + data + "' and codigoProfessor = " + lstProfessor.SelectedValue + " and linha = 3 and aulaRealizada <> 1";

                        SqlCommand cmd = new SqlCommand(selectDeletar, sqlc);
                        SqlDataReader dr = cmd.ExecuteReader();

                        if (dr.HasRows)
                        {
                            while (dr.Read())
                            {
                                string delete = "delete from aula where codigoAula = " + dr["codigoAula"].ToString();

                                SqlCommand cmdDeletar = new SqlCommand(delete, sqld);
                                cmdDeletar.ExecuteNonQuery();
                            }
                        }

                    }

                    sqlc.Close();
                    sqld.Close();
                }
            }
        }

        void linha4(string selectVerificar, string data, string ano, string professor, string hora1Inicial, string hora1Final, string hora2Inicial, string hora2Final, string hora3Inicial, string hora3Final, string hora4Inicial, string hora4Final, string hora5Inicial, string hora5Final, string hora6Inicial, string hora6Final, string hora7Inicial, string hora7Final, string hora8Inicial, string hora8Final, bool mensagem)
        {
            SqlConnection sqlc = new SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
            sqlc.Open();
            SqlConnection sqld = new SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
            sqld.Open();

            //INSERT E VALIDAÇÕES REFERENTES A 1ª LINHA DE AGENDA DE AULA
            if (hide4.Value != "0" || lstgrupo4.SelectedValue != "0")
            {
                if (hide4.Value != "0" && lstgrupo4.SelectedValue != "0")
                {
                    //validação selecione um grupo ou um aluno
                    hide4.Value = "0";
                    lstgrupo4.SelectedValue = "0";
                    lstmateria4.SelectedValue = "0";
                    //mensagem java script
                    string alunoGrupo = "<script language=JavaScript>alunoGrupo4();</script>";
                    Panel2.Controls.Add(new LiteralControl(alunoGrupo));
                }
                else
                {
                    string horaInicial = inicial4Hora4.Text;
                    string horaFinal = final4Hora4.Text;



                    if ((horaInicial.Length == 5 && horaFinal.Length == 5))
                    {






                        if (horaInicial.Length == 4)
                        {
                            horaInicial = "0" + horaInicial;
                        }
                        if (horaFinal.Length == 4)
                        {
                            horaFinal = "0" + horaFinal;
                        }

                        TimeSpan dif = Convert.ToDateTime(horaFinal).Subtract(Convert.ToDateTime(horaInicial));

                        if (dif.Hours <= 0)
                        {
                            //validação horas iguais

                            //mensagem java script
                            string horasIguais = "<script language=JavaScript>horasIguais4();</script>";
                            Panel2.Controls.Add(new LiteralControl(horasIguais));
                        }
                        else
                        {
                            bool validarInsert = true;

                            SqlCommand cmdVerificar = new SqlCommand(selectVerificar + " and linha <> '4'", sqlc);
                            SqlDataReader drVerificar = cmdVerificar.ExecuteReader();

                            if (drVerificar.HasRows)
                            {
                                while (drVerificar.Read())
                                {
                                    hora1Inicial = drVerificar["horaInicial1"].ToString();
                                    hora1Final = drVerificar["horaFinal1"].ToString();

                                    hora2Inicial = drVerificar["horaInicial2"].ToString();
                                    hora2Final = drVerificar["horaFinal2"].ToString();

                                    hora3Inicial = drVerificar["horaInicial3"].ToString();
                                    hora3Final = drVerificar["horaFinal3"].ToString();

                                    hora4Inicial = drVerificar["horaInicial4"].ToString();
                                    hora4Final = drVerificar["horaFinal4"].ToString();

                                    hora5Inicial = drVerificar["horaInicial5"].ToString();
                                    hora5Final = drVerificar["horaFinal5"].ToString();

                                    hora6Inicial = drVerificar["horaInicial6"].ToString();
                                    hora6Final = drVerificar["horaFinal6"].ToString();

                                    hora7Inicial = drVerificar["horaInicial7"].ToString();
                                    hora7Final = drVerificar["horaFinal7"].ToString();

                                    hora8Inicial = drVerificar["horaInicial8"].ToString();
                                    hora8Final = drVerificar["horaFinal8"].ToString();


                                    switch (drVerificar["linha"].ToString())
                                    {
                                        case "2":
                                            if (hora2Inicial != "" && hora2Final != "")
                                            {
                                                if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial2"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal2"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial2"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal2"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial2"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal2"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }
                                            }
                                            break;
                                        case "3":
                                            if (hora3Inicial != "" && hora3Final != "")
                                            {
                                                if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial3"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal3"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial3"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal3"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial3"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal3"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }
                                            }
                                            break;
                                        case "1":
                                            if (hora1Inicial != "" && hora1Final != "")
                                            {
                                                if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial1"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal1"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial1"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal1"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial1"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal1"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }
                                            }
                                            break;

                                        case "5":
                                            if (hora5Inicial != "" && hora5Final != "")
                                            {
                                                if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial5"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal5"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial5"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal5"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial5"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal5"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }
                                            }
                                            break;

                                        case "6":
                                            if (hora6Inicial != "" && hora6Final != "")
                                            {
                                                if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial6"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal6"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial6"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal6"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial6"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal6"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }
                                            }
                                            break;

                                        case "7":
                                            if (hora7Inicial != "" && hora7Final != "")
                                            {
                                                if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial7"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal7"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial7"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal7"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial7"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal7"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }
                                            }
                                            break;

                                        case "8":
                                            if (hora8Inicial != "" && hora8Final != "")
                                            {
                                                if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial8"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal8"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial8"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal8"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial8"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal8"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }
                                            }
                                            break;
                                        default:
                                            break;
                                    }
                                }
                            }
                            drVerificar.Close();

                            if (validarInsert)
                            {
                                string sqlInserirAula = "";
                                if (verificarAulaAluno(hide4.Value, lstgrupo4.SelectedValue, data, horaInicial, horaFinal, "4", lstProfessor.SelectedValue))
                                {
                                    if (hide4.Value != "0")
                                    {
                                        DateTime hI = Convert.ToDateTime(horaInicial);

                                        string sqlDeletarAula = "delete from aula where linha = '4' and codigoProfessor = " + lstProfessor.SelectedValue + " and dataAula = '" + data + "'";

                                        SqlCommand cmdDelete = new SqlCommand(sqlDeletarAula, sqld);
                                        cmdDelete.ExecuteNonQuery();
                                        if ((lstmateria4.SelectedValue != "0"))
                                        {
                                            int i = 0;
                                            while (i < dif.TotalHours)
                                            {
                                                DateTime hF = hI;
                                                hF += new TimeSpan(0, 0, 3600);

                                                string horaI = hI.ToString("HH:mm");
                                                string horaF = hF.ToString("HH:mm");

                                                if (hI.ToString().Length == 4)
                                                {
                                                    horaI = "0" + hI.ToString("HH:mm");
                                                }
                                                if (hF.ToString().Length == 4)
                                                {
                                                    horaF = "0" + hF.ToString("HH:mm");
                                                }

                                                sqlInserirAula = "INSERT INTO aula ";
                                                sqlInserirAula = sqlInserirAula + "(codigoDisciplina, codigoProfessor, horaInicialAula, horaFinalAula, dataAula, codigoAluno, codigoAnoLetivo, linha, meiaHora, codigoTipoAula)";
                                                sqlInserirAula = sqlInserirAula + " VALUES (" + lstmateria4.SelectedValue + ", " + lstProfessor.SelectedValue + ", ";
                                                sqlInserirAula = sqlInserirAula + " '" + horaI + "', '" + horaF + "', '" + data + "', " + hide4.Value + ", " + ano + ",'4', 0, 1)";

                                                if (Convert.ToDateTime(horaFinal) != hF)
                                                {
                                                    hI = hF;
                                                }

                                                SqlCommand cmdInsert = new SqlCommand(sqlInserirAula, sqld);
                                                cmdInsert.ExecuteNonQuery();
                                                ///////////////////////////////////////////////////////AQUI
                                                i++;
                                                if ((Convert.ToDouble(dif.TotalHours) - i) == 0.5)
                                                {
                                                    hF = hI;
                                                    hF += new TimeSpan(0, 0, 1800);

                                                    horaI = hI.ToString("HH:mm");
                                                    horaF = hF.ToString("HH:mm");

                                                    if (hI.ToString().Length == 4)
                                                    {
                                                        horaI = "0" + hI.ToString("HH:mm");
                                                    }
                                                    if (hF.ToString().Length == 4)
                                                    {
                                                        horaF = "0" + hF.ToString("HH:mm");
                                                    }

                                                    sqlInserirAula = "INSERT INTO aula ";
                                                    sqlInserirAula = sqlInserirAula + "(codigoDisciplina, codigoProfessor, horaInicialAula, horaFinalAula, dataAula, codigoAluno, codigoAnoLetivo, linha, meiaHora, codigoTipoAula)";
                                                    sqlInserirAula = sqlInserirAula + " VALUES (" + lstmateria4.SelectedValue + ", " + lstProfessor.SelectedValue + ", ";
                                                    sqlInserirAula = sqlInserirAula + " '" + horaI + "', '" + horaF + "', '" + data + "', " + hide4.Value + ", " + ano + ", '4', 1, 1)";

                                                    cmdInsert = new SqlCommand(sqlInserirAula, sqld);
                                                    cmdInsert.ExecuteNonQuery();
                                                    break;
                                                }
                                            }
                                        }

                                        //mensagem java script
                                        string finalizar = "<script language=JavaScript>finalizar4();</script>";
                                        Panel2.Controls.Add(new LiteralControl(finalizar));

                                        mensagem = false;
                                    }
                                    else
                                    {
                                        DateTime hI = Convert.ToDateTime(horaInicial);

                                        string sqlDeletarAula = "delete from aula where linha = '4' and codigoProfessor = " + lstProfessor.SelectedValue + " and dataAula = '" + data + "'";

                                        SqlCommand cmdDelete = new SqlCommand(sqlDeletarAula, sqld);
                                        cmdDelete.ExecuteNonQuery();

                                        if ((lstmateria4.SelectedValue != "0"))
                                        {
                                            int i = 0;
                                            while (i < dif.TotalHours)
                                            {
                                                DateTime hF = hI;
                                                hF += new TimeSpan(0, 0, 3600);

                                                string horaI = hI.ToString("HH:mm");
                                                string horaF = hF.ToString("HH:mm");

                                                if (hI.ToString().Length == 4)
                                                {
                                                    horaI = "0" + hI.ToString("HH:mm");
                                                }
                                                if (hF.ToString().Length == 4)
                                                {
                                                    horaF = "0" + hF.ToString("HH:mm");
                                                }
                                                sqlInserirAula = "INSERT INTO aula ";
                                                sqlInserirAula = sqlInserirAula + "(codigoDisciplina, codigoProfessor, horaInicialAula, horaFinalAula, dataAula, codigoAnoLetivo, codigoGrupo, linha, meiaHora, codigoTipoAula)";
                                                sqlInserirAula = sqlInserirAula + " VALUES (" + lstmateria4.SelectedValue + ", " + lstProfessor.SelectedValue + ", ";
                                                sqlInserirAula = sqlInserirAula + " '" + horaI + "', '" + horaF + "', '" + data + "', " + ano + ", " + lstgrupo4.SelectedValue + ", '4', 0, 2)";

                                                if (Convert.ToDateTime(horaFinal) != hF)
                                                {
                                                    hI = hF;
                                                }

                                                SqlCommand cmdInsert = new SqlCommand(sqlInserirAula, sqld);
                                                cmdInsert.ExecuteNonQuery();

                                                i++;
                                                if ((Convert.ToDouble(dif.TotalHours) - i) == 0.5)
                                                {
                                                    hF = hI;
                                                    hF += new TimeSpan(0, 0, 1800);

                                                    horaI = hI.ToString("HH:mm");
                                                    horaF = hF.ToString("HH:mm");

                                                    if (hI.ToString().Length == 4)
                                                    {
                                                        horaI = "0" + hI.ToString("HH:mm");
                                                    }
                                                    if (hF.ToString().Length == 4)
                                                    {
                                                        horaF = "0" + hF.ToString("HH:mm");
                                                    }

                                                    sqlInserirAula = "INSERT INTO aula ";
                                                    sqlInserirAula = sqlInserirAula + "(codigoDisciplina, codigoProfessor, horaInicialAula, horaFinalAula, dataAula, codigoAnoLetivo, codigoGrupo, linha, meiaHora, codigoTipoAula)";
                                                    sqlInserirAula = sqlInserirAula + " VALUES (" + lstmateria4.SelectedValue + ", " + lstProfessor.SelectedValue + ", ";
                                                    sqlInserirAula = sqlInserirAula + " '" + horaI + "', '" + horaF + "', '" + data + "', " + ano + ", " + lstgrupo4.SelectedValue + ",'4', 1, 2)";

                                                    cmdInsert = new SqlCommand(sqlInserirAula, sqld);
                                                    cmdInsert.ExecuteNonQuery();
                                                    break;
                                                }
                                            }
                                        }

                                        //mensagem java script
                                        string finalizar = "<script language=JavaScript>finalizar4();</script>";
                                        Panel2.Controls.Add(new LiteralControl(finalizar));

                                        mensagem = false;
                                    }
                                }
                            }
                            else
                            {
                                //validar já existe aula nessas horas

                                //mensagem java script
                                string existe = "<script language=JavaScript>existe4();</script>";
                                Panel2.Controls.Add(new LiteralControl(existe));
                            }
                        }
                    }




                    else
                    {

                        if ((horaInicial != "" && horaFinal != ""))
                        {
                            //mensagem java script
                            string existe = "<script language=JavaScript>alert('As horas inicial e final devem estar no formato hh:mm.');</script>";
                            Panel2.Controls.Add(new LiteralControl(existe));

                            if (horaFinal.Length != 5)
                            {

                                final4Hora4.Focus();

                            }


                            if (horaInicial.Length != 5)
                            {

                                inicial4Hora4.Focus();

                            }


                        }
                    }














                    {
                        string selectDeletar = "select * from aula where dataAula = '" + data + "' and codigoProfessor = " + lstProfessor.SelectedValue + " and linha = 4 and aulaRealizada <> 1";

                        SqlCommand cmd = new SqlCommand(selectDeletar, sqlc);
                        SqlDataReader dr = cmd.ExecuteReader();

                        if (dr.HasRows)
                        {
                            while (dr.Read())
                            {
                                string delete = "delete from aula where codigoAula = " + dr["codigoAula"].ToString();

                                SqlCommand cmdDeletar = new SqlCommand(delete, sqld);
                                cmdDeletar.ExecuteNonQuery();
                            }
                        }

                        string finalizar = "<script language=JavaScript>finalizar4();</script>";
                        Panel2.Controls.Add(new LiteralControl(finalizar));
                    }

                    sqlc.Close();
                    sqld.Close();
                }
            }
        }

        void linha5(string selectVerificar, string data, string ano, string professor, string hora1Inicial, string hora1Final, string hora2Inicial, string hora2Final, string hora3Inicial, string hora3Final, string hora4Inicial, string hora4Final, string hora5Inicial, string hora5Final, string hora6Inicial, string hora6Final, string hora7Inicial, string hora7Final, string hora8Inicial, string hora8Final, bool mensagem)
        {
            SqlConnection sqlc = new SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
            sqlc.Open();
            SqlConnection sqld = new SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
            sqld.Open();

            //INSERT E VALIDAÇÕES REFERENTES A 1ª LINHA DE AGENDA DE AULA
            if (hide5.Value != "0" || lstgrupo5.SelectedValue != "0")
            {
                if (hide5.Value != "0" && lstgrupo5.SelectedValue != "0")
                {
                    //validação selecione um grupo ou um aluno
                    hide5.Value = "0";
                    lstgrupo5.SelectedValue = "0";
                    lstmateria5.SelectedValue = "0";
                    //mensagem java script
                    string alunoGrupo = "<script language=JavaScript>alunoGrupo5();</script>";
                    Panel2.Controls.Add(new LiteralControl(alunoGrupo));
                }
                else
                {
                    string horaInicial = inicial5Hora5.Text;
                    string horaFinal = final5Hora5.Text;




                    if ((horaInicial.Length == 5 && horaFinal.Length == 5))
                    {







                        if (horaInicial.Length == 4)
                        {
                            horaInicial = "0" + horaInicial;
                        }
                        if (horaFinal.Length == 4)
                        {
                            horaFinal = "0" + horaFinal;
                        }

                        TimeSpan dif = Convert.ToDateTime(horaFinal).Subtract(Convert.ToDateTime(horaInicial));

                        if (dif.Hours <= 0)
                        {
                            //validação horas iguais

                            //mensagem java script
                            string horasIguais = "<script language=JavaScript>horasIguais5();</script>";
                            Panel2.Controls.Add(new LiteralControl(horasIguais));
                        }
                        else
                        {
                            bool validarInsert = true;

                            SqlCommand cmdVerificar = new SqlCommand(selectVerificar + " and linha <> '5'", sqlc);
                            SqlDataReader drVerificar = cmdVerificar.ExecuteReader();

                            if (drVerificar.HasRows)
                            {
                                while (drVerificar.Read())
                                {
                                    hora1Inicial = drVerificar["horaInicial1"].ToString();
                                    hora1Final = drVerificar["horaFinal1"].ToString();

                                    hora2Inicial = drVerificar["horaInicial2"].ToString();
                                    hora2Final = drVerificar["horaFinal2"].ToString();

                                    hora3Inicial = drVerificar["horaInicial3"].ToString();
                                    hora3Final = drVerificar["horaFinal3"].ToString();

                                    hora4Inicial = drVerificar["horaInicial4"].ToString();
                                    hora4Final = drVerificar["horaFinal4"].ToString();

                                    hora5Inicial = drVerificar["horaInicial5"].ToString();
                                    hora5Final = drVerificar["horaFinal5"].ToString();

                                    hora6Inicial = drVerificar["horaInicial6"].ToString();
                                    hora6Final = drVerificar["horaFinal6"].ToString();

                                    hora7Inicial = drVerificar["horaInicial7"].ToString();
                                    hora7Final = drVerificar["horaFinal7"].ToString();

                                    hora8Inicial = drVerificar["horaInicial8"].ToString();
                                    hora8Final = drVerificar["horaFinal8"].ToString();


                                    switch (drVerificar["linha"].ToString())
                                    {
                                        case "2":
                                            if (hora2Inicial != "" && hora2Final != "")
                                            {
                                                if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial2"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal2"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial2"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal2"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial2"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal2"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }
                                            }
                                            break;
                                        case "3":
                                            if (hora3Inicial != "" && hora3Final != "")
                                            {
                                                if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial3"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal3"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial3"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal3"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial3"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal3"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }
                                            }
                                            break;
                                        case "4":
                                            if (hora4Inicial != "" && hora4Final != "")
                                            {
                                                if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial4"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal4"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial4"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal4"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial4"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal4"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }
                                            }
                                            break;

                                        case "1":
                                            if (hora1Inicial != "" && hora1Final != "")
                                            {
                                                if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial1"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal1"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial1"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal1"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial1"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal1"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }
                                            }
                                            break;

                                        case "6":
                                            if (hora6Inicial != "" && hora6Final != "")
                                            {
                                                if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial6"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal6"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial6"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal6"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial6"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal6"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }
                                            }
                                            break;

                                        case "7":
                                            if (hora7Inicial != "" && hora7Final != "")
                                            {
                                                if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial7"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal7"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial7"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal7"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial7"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal7"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }
                                            }
                                            break;

                                        case "8":
                                            if (hora8Inicial != "" && hora8Final != "")
                                            {
                                                if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial8"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal8"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial8"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal8"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial8"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal8"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }
                                            }
                                            break;
                                        default:
                                            break;
                                    }
                                }
                            }
                            drVerificar.Close();

                            if (validarInsert)
                            {
                                string sqlInserirAula = "";
                                if (verificarAulaAluno(hide5.Value, lstgrupo5.SelectedValue, data, horaInicial, horaFinal, "5", lstProfessor.SelectedValue))
                                {
                                    if (hide5.Value != "0")
                                    {
                                        DateTime hI = Convert.ToDateTime(horaInicial);

                                        string sqlDeletarAula = "delete from aula where linha = '5' and codigoProfessor = " + lstProfessor.SelectedValue + " and dataAula = '" + data + "'";

                                        SqlCommand cmdDelete = new SqlCommand(sqlDeletarAula, sqld);
                                        cmdDelete.ExecuteNonQuery();
                                        if ((lstmateria5.SelectedValue != "0"))
                                        {
                                            int i = 0;
                                            while (i < dif.TotalHours)
                                            {
                                                DateTime hF = hI;
                                                hF += new TimeSpan(0, 0, 3600);

                                                string horaI = hI.ToString("HH:mm");
                                                string horaF = hF.ToString("HH:mm");

                                                if (hI.ToString().Length == 4)
                                                {
                                                    horaI = "0" + hI.ToString("HH:mm");
                                                }
                                                if (hF.ToString().Length == 4)
                                                {
                                                    horaF = "0" + hF.ToString("HH:mm");
                                                }

                                                sqlInserirAula = "INSERT INTO aula ";
                                                sqlInserirAula = sqlInserirAula + "(codigoDisciplina, codigoProfessor, horaInicialAula, horaFinalAula, dataAula, codigoAluno, codigoAnoLetivo, linha, meiaHora, codigoTipoAula)";
                                                sqlInserirAula = sqlInserirAula + " VALUES (" + lstmateria5.SelectedValue + ", " + lstProfessor.SelectedValue + ", ";
                                                sqlInserirAula = sqlInserirAula + " '" + horaI + "', '" + horaF + "', '" + data + "', " + hide5.Value + ", " + ano + ", '5', 0, 1)";

                                                if (Convert.ToDateTime(horaFinal) != hF)
                                                {
                                                    hI = hF;
                                                }

                                                SqlCommand cmdInsert = new SqlCommand(sqlInserirAula, sqld);
                                                cmdInsert.ExecuteNonQuery();
                                                ///////////////////////////////////////////////////////AQUI
                                                i++;
                                                if ((Convert.ToDouble(dif.TotalHours) - i) == 0.5)
                                                {
                                                    hF = hI;
                                                    hF += new TimeSpan(0, 0, 1800);

                                                    horaI = hI.ToString("HH:mm");
                                                    horaF = hF.ToString("HH:mm");

                                                    if (hI.ToString().Length == 4)
                                                    {
                                                        horaI = "0" + hI.ToString("HH:mm");
                                                    }
                                                    if (hF.ToString().Length == 4)
                                                    {
                                                        horaF = "0" + hF.ToString("HH:mm");
                                                    }

                                                    sqlInserirAula = "INSERT INTO aula ";
                                                    sqlInserirAula = sqlInserirAula + "(codigoDisciplina, codigoProfessor, horaInicialAula, horaFinalAula, dataAula, codigoAluno, codigoAnoLetivo, linha, meiaHora, codigoTipoAula)";
                                                    sqlInserirAula = sqlInserirAula + " VALUES (" + lstmateria5.SelectedValue + ", " + lstProfessor.SelectedValue + ", ";
                                                    sqlInserirAula = sqlInserirAula + " '" + horaI + "', '" + horaF + "', '" + data + "', " + hide5.Value + ", " + ano + ", '5', 1, 1)";

                                                    cmdInsert = new SqlCommand(sqlInserirAula, sqld);
                                                    cmdInsert.ExecuteNonQuery();
                                                    break;
                                                }
                                            }
                                        }

                                        //mensagem java script
                                        string finalizar = "<script language=JavaScript>finalizar5();</script>";
                                        Panel2.Controls.Add(new LiteralControl(finalizar));

                                        mensagem = false;
                                    }
                                    else
                                    {
                                        DateTime hI = Convert.ToDateTime(horaInicial);

                                        string sqlDeletarAula = "delete from aula where linha = '5' and codigoProfessor = " + lstProfessor.SelectedValue + " and dataAula = '" + data + "'";

                                        SqlCommand cmdDelete = new SqlCommand(sqlDeletarAula, sqld);
                                        cmdDelete.ExecuteNonQuery();

                                        if ((lstmateria5.SelectedValue != "0"))
                                        {
                                            int i = 0;
                                            while (i < dif.TotalHours)
                                            {
                                                DateTime hF = hI;
                                                hF += new TimeSpan(0, 0, 3600);

                                                string horaI = hI.ToString("HH:mm");
                                                string horaF = hF.ToString("HH:mm");

                                                if (hI.ToString().Length == 4)
                                                {
                                                    horaI = "0" + hI.ToString("HH:mm");
                                                }
                                                if (hF.ToString().Length == 4)
                                                {
                                                    horaF = "0" + hF.ToString("HH:mm");
                                                }
                                                sqlInserirAula = "INSERT INTO aula ";
                                                sqlInserirAula = sqlInserirAula + "(codigoDisciplina, codigoProfessor, horaInicialAula, horaFinalAula, dataAula, codigoAnoLetivo, codigoGrupo, linha, meiaHora, codigoTipoAula)";
                                                sqlInserirAula = sqlInserirAula + " VALUES (" + lstmateria5.SelectedValue + ", " + lstProfessor.SelectedValue + ", ";
                                                sqlInserirAula = sqlInserirAula + " '" + horaI + "', '" + horaF + "', '" + data + "', " + ano + ", " + lstgrupo5.SelectedValue + ", '5', 0, 2)";

                                                if (Convert.ToDateTime(horaFinal) != hF)
                                                {
                                                    hI = hF;
                                                }

                                                SqlCommand cmdInsert = new SqlCommand(sqlInserirAula, sqld);
                                                cmdInsert.ExecuteNonQuery();

                                                i++;
                                                if ((Convert.ToDouble(dif.TotalHours) - i) == 0.5)
                                                {
                                                    hF = hI;
                                                    hF += new TimeSpan(0, 0, 1800);

                                                    horaI = hI.ToString("HH:mm");
                                                    horaF = hF.ToString("HH:mm");

                                                    if (hI.ToString().Length == 4)
                                                    {
                                                        horaI = "0" + hI.ToString("HH:mm");
                                                    }
                                                    if (hF.ToString().Length == 4)
                                                    {
                                                        horaF = "0" + hF.ToString("HH:mm");
                                                    }

                                                    sqlInserirAula = "INSERT INTO aula ";
                                                    sqlInserirAula = sqlInserirAula + "(codigoDisciplina, codigoProfessor, horaInicialAula, horaFinalAula, dataAula, codigoAnoLetivo, codigoGrupo, linha, meiaHora, codigoTipoAula)";
                                                    sqlInserirAula = sqlInserirAula + " VALUES (" + lstmateria5.SelectedValue + ", " + lstProfessor.SelectedValue + ", ";
                                                    sqlInserirAula = sqlInserirAula + " '" + horaI + "', '" + horaF + "', '" + data + "', " + ano + ", " + lstgrupo5.SelectedValue + ", '5', 1, 2)";

                                                    cmdInsert = new SqlCommand(sqlInserirAula, sqld);
                                                    cmdInsert.ExecuteNonQuery();
                                                    break;
                                                }
                                            }
                                        }

                                        //mensagem java script
                                        string finalizar = "<script language=JavaScript>finalizar5();</script>";
                                        Panel2.Controls.Add(new LiteralControl(finalizar));

                                        mensagem = false;
                                    }
                                }
                            }
                            else
                            {
                                //validar já existe aula nessas horas

                                //mensagem java script
                                string existe = "<script language=JavaScript>existe5();</script>";
                                Panel2.Controls.Add(new LiteralControl(existe));
                            }
                        }
                    }




                    else
                    {
                        if ((horaInicial != "" && horaFinal != ""))
                        {

                            //mensagem java script
                            string existe = "<script language=JavaScript>alert('As horas inicial e final devem estar no formato hh:mm.');</script>";
                            Panel2.Controls.Add(new LiteralControl(existe));

                            if (horaFinal.Length != 5)
                            {

                                final5Hora5.Focus();

                            }


                            if (horaInicial.Length != 5)
                            {

                                inicial5Hora5.Focus();

                            }


                        }
                    }













                    {
                        string selectDeletar = "select * from aula where dataAula = '" + data + "' and codigoProfessor = " + lstProfessor.SelectedValue + " and linha = 5 and aulaRealizada <> 1";

                        SqlCommand cmd = new SqlCommand(selectDeletar, sqlc);
                        SqlDataReader dr = cmd.ExecuteReader();

                        if (dr.HasRows)
                        {
                            while (dr.Read())
                            {
                                string delete = "delete from aula where codigoAula = " + dr["codigoAula"].ToString();

                                SqlCommand cmdDeletar = new SqlCommand(delete, sqld);
                                cmdDeletar.ExecuteNonQuery();
                            }
                        }

                        string finalizar = "<script language=JavaScript>finalizar5();</script>";
                        Panel2.Controls.Add(new LiteralControl(finalizar));
                    }

                    sqlc.Close();
                    sqld.Close();
                }
            }
        }

        void linha6(string selectVerificar, string data, string ano, string professor, string hora1Inicial, string hora1Final, string hora2Inicial, string hora2Final, string hora3Inicial, string hora3Final, string hora4Inicial, string hora4Final, string hora5Inicial, string hora5Final, string hora6Inicial, string hora6Final, string hora7Inicial, string hora7Final, string hora8Inicial, string hora8Final, bool mensagem)
        {
            SqlConnection sqlc = new SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
            sqlc.Open();
            SqlConnection sqld = new SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
            sqld.Open();

            //INSERT E VALIDAÇÕES REFERENTES A 1ª LINHA DE AGENDA DE AULA
            if (hide6.Value != "0" || lstgrupo6.SelectedValue != "0")
            {
                if (hide6.Value != "0" && lstgrupo6.SelectedValue != "0")
                {
                    //validação selecione um grupo ou um aluno
                    hide6.Value = "0";
                    lstgrupo6.SelectedValue = "0";
                    lstmateria6.SelectedValue = "0";
                    //mensagem java script
                    string alunoGrupo = "<script language=JavaScript>alunoGrupo6();</script>";
                    Panel2.Controls.Add(new LiteralControl(alunoGrupo));
                }
                else
                {
                    string horaInicial = inicial6Hora6.Text;
                    string horaFinal = final6Hora6.Text;


                    if ((horaInicial.Length == 5 && horaFinal.Length == 5))
                    {








                        if (horaInicial.Length == 4)
                        {
                            horaInicial = "0" + horaInicial;
                        }
                        if (horaFinal.Length == 4)
                        {
                            horaFinal = "0" + horaFinal;
                        }

                        TimeSpan dif = Convert.ToDateTime(horaFinal).Subtract(Convert.ToDateTime(horaInicial));

                        if (dif.Hours <= 0)
                        {
                            //validação horas iguais

                            //mensagem java script
                            string horasIguais = "<script language=JavaScript>horasIguais6();</script>";
                            Panel2.Controls.Add(new LiteralControl(horasIguais));
                        }
                        else
                        {
                            bool validarInsert = true;

                            SqlCommand cmdVerificar = new SqlCommand(selectVerificar + " and linha <> '6'", sqlc);
                            SqlDataReader drVerificar = cmdVerificar.ExecuteReader();

                            if (drVerificar.HasRows)
                            {
                                while (drVerificar.Read())
                                {
                                    hora1Inicial = drVerificar["horaInicial1"].ToString();
                                    hora1Final = drVerificar["horaFinal1"].ToString();

                                    hora2Inicial = drVerificar["horaInicial2"].ToString();
                                    hora2Final = drVerificar["horaFinal2"].ToString();

                                    hora3Inicial = drVerificar["horaInicial3"].ToString();
                                    hora3Final = drVerificar["horaFinal3"].ToString();

                                    hora4Inicial = drVerificar["horaInicial4"].ToString();
                                    hora4Final = drVerificar["horaFinal4"].ToString();

                                    hora5Inicial = drVerificar["horaInicial5"].ToString();
                                    hora5Final = drVerificar["horaFinal5"].ToString();

                                    hora6Inicial = drVerificar["horaInicial6"].ToString();
                                    hora6Final = drVerificar["horaFinal6"].ToString();

                                    hora7Inicial = drVerificar["horaInicial7"].ToString();
                                    hora7Final = drVerificar["horaFinal7"].ToString();

                                    hora8Inicial = drVerificar["horaInicial8"].ToString();
                                    hora8Final = drVerificar["horaFinal8"].ToString();


                                    switch (drVerificar["linha"].ToString())
                                    {
                                        case "2":
                                            if (hora2Inicial != "" && hora2Final != "")
                                            {
                                                if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial2"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal2"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial2"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal2"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial2"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal2"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }
                                            }
                                            break;
                                        case "3":
                                            if (hora3Inicial != "" && hora3Final != "")
                                            {
                                                if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial3"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal3"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial3"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal3"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial3"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal3"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }
                                            }
                                            break;
                                        case "4":
                                            if (hora4Inicial != "" && hora4Final != "")
                                            {
                                                if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial4"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal4"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial4"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal4"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial4"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal4"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }
                                            }
                                            break;

                                        case "5":
                                            if (hora5Inicial != "" && hora5Final != "")
                                            {
                                                if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial5"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal5"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial5"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal5"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial5"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal5"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }
                                            }
                                            break;

                                        case "1":
                                            if (hora1Inicial != "" && hora1Final != "")
                                            {
                                                if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial1"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal1"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial1"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal1"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial1"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal1"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }
                                            }
                                            break;

                                        case "7":
                                            if (hora7Inicial != "" && hora7Final != "")
                                            {
                                                if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial7"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal7"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial7"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal7"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial7"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal7"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }
                                            }
                                            break;

                                        case "8":
                                            if (hora8Inicial != "" && hora8Final != "")
                                            {
                                                if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial8"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal8"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial8"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal8"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial8"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal8"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }
                                            }
                                            break;
                                        default:
                                            break;
                                    }
                                }
                            }
                            drVerificar.Close();

                            if (validarInsert)
                            {
                                string sqlInserirAula = "";
                                if (verificarAulaAluno(hide6.Value, lstgrupo6.SelectedValue, data, horaInicial, horaFinal, "6", lstProfessor.SelectedValue))
                                {
                                    if (hide6.Value != "0")
                                    {
                                        DateTime hI = Convert.ToDateTime(horaInicial);

                                        string sqlDeletarAula = "delete from aula where linha = '6' and codigoProfessor = " + lstProfessor.SelectedValue + " and dataAula = '" + data + "'";

                                        SqlCommand cmdDelete = new SqlCommand(sqlDeletarAula, sqld);
                                        cmdDelete.ExecuteNonQuery();
                                        if ((lstmateria6.SelectedValue != "0"))
                                        {
                                            int i = 0;
                                            while (i < dif.TotalHours)
                                            {
                                                DateTime hF = hI;
                                                hF += new TimeSpan(0, 0, 3600);

                                                string horaI = hI.ToString("HH:mm");
                                                string horaF = hF.ToString("HH:mm");

                                                if (hI.ToString().Length == 4)
                                                {
                                                    horaI = "0" + hI.ToString("HH:mm");
                                                }
                                                if (hF.ToString().Length == 4)
                                                {
                                                    horaF = "0" + hF.ToString("HH:mm");
                                                }

                                                sqlInserirAula = "INSERT INTO aula ";
                                                sqlInserirAula = sqlInserirAula + "(codigoDisciplina, codigoProfessor, horaInicialAula, horaFinalAula, dataAula, codigoAluno, codigoAnoLetivo, linha, meiaHora, codigoTipoAula)";
                                                sqlInserirAula = sqlInserirAula + " VALUES (" + lstmateria6.SelectedValue + ", " + lstProfessor.SelectedValue + ", ";
                                                sqlInserirAula = sqlInserirAula + " '" + horaI + "', '" + horaF + "', '" + data + "', " + hide6.Value + ", " + ano + ", '6', 0, 1)";

                                                if (Convert.ToDateTime(horaFinal) != hF)
                                                {
                                                    hI = hF;
                                                }

                                                SqlCommand cmdInsert = new SqlCommand(sqlInserirAula, sqld);
                                                cmdInsert.ExecuteNonQuery();

                                                i++;
                                                if ((Convert.ToDouble(dif.TotalHours) - i) == 0.5)
                                                {
                                                    hF = hI;
                                                    hF += new TimeSpan(0, 0, 1800);

                                                    horaI = hI.ToString("HH:mm");
                                                    horaF = hF.ToString("HH:mm");

                                                    if (hI.ToString().Length == 4)
                                                    {
                                                        horaI = "0" + hI.ToString("HH:mm");
                                                    }
                                                    if (hF.ToString().Length == 4)
                                                    {
                                                        horaF = "0" + hF.ToString("HH:mm");
                                                    }

                                                    sqlInserirAula = "INSERT INTO aula ";
                                                    sqlInserirAula = sqlInserirAula + "(codigoDisciplina, codigoProfessor, horaInicialAula, horaFinalAula, dataAula, codigoAluno, codigoAnoLetivo, linha, meiaHora, codigoTipoAula)";
                                                    sqlInserirAula = sqlInserirAula + " VALUES (" + lstmateria6.SelectedValue + ", " + lstProfessor.SelectedValue + ", ";
                                                    sqlInserirAula = sqlInserirAula + " '" + horaI + "', '" + horaF + "', '" + data + "', " + hide6.Value + ", " + ano + ", '6', 1, 1)";

                                                    cmdInsert = new SqlCommand(sqlInserirAula, sqld);
                                                    cmdInsert.ExecuteNonQuery();
                                                    break;
                                                }
                                            }
                                        }

                                        //mensagem java script
                                        string finalizar = "<script language=JavaScript>finalizar3();</script>";
                                        Panel2.Controls.Add(new LiteralControl(finalizar));

                                        mensagem = false;
                                    }
                                    else
                                    {
                                        DateTime hI = Convert.ToDateTime(horaInicial);

                                        string sqlDeletarAula = "delete from aula where linha = '6' and codigoProfessor = " + lstProfessor.SelectedValue + " and dataAula = '" + data + "'";

                                        SqlCommand cmdDelete = new SqlCommand(sqlDeletarAula, sqld);
                                        cmdDelete.ExecuteNonQuery();

                                        if ((lstmateria6.SelectedValue != "0"))
                                        {
                                            int i = 0;
                                            while (i < dif.TotalHours)
                                            {
                                                DateTime hF = hI;
                                                hF += new TimeSpan(0, 0, 3600);

                                                string horaI = hI.ToString("HH:mm");
                                                string horaF = hF.ToString("HH:mm");

                                                if (hI.ToString().Length == 4)
                                                {
                                                    horaI = "0" + hI.ToString("HH:mm");
                                                }
                                                if (hF.ToString().Length == 4)
                                                {
                                                    horaF = "0" + hF.ToString("HH:mm");
                                                }
                                                sqlInserirAula = "INSERT INTO aula ";
                                                sqlInserirAula = sqlInserirAula + "(codigoDisciplina, codigoProfessor, horaInicialAula, horaFinalAula, dataAula, codigoAnoLetivo, codigoGrupo, linha, meiaHora, codigoTipoAula)";
                                                sqlInserirAula = sqlInserirAula + " VALUES (" + lstmateria6.SelectedValue + ", " + lstProfessor.SelectedValue + ", ";
                                                sqlInserirAula = sqlInserirAula + " '" + horaI + "', '" + horaF + "', '" + data + "', " + ano + ", " + lstgrupo6.SelectedValue + ", '6', 0, 2)";

                                                if (Convert.ToDateTime(horaFinal) != hF)
                                                {
                                                    hI = hF;
                                                }

                                                SqlCommand cmdInsert = new SqlCommand(sqlInserirAula, sqld);
                                                cmdInsert.ExecuteNonQuery();

                                                i++;
                                                if ((Convert.ToDouble(dif.TotalHours) - i) == 0.5)
                                                {
                                                    hF = hI;
                                                    hF += new TimeSpan(0, 0, 1800);

                                                    horaI = hI.ToString("HH:mm");
                                                    horaF = hF.ToString("HH:mm");

                                                    if (hI.ToString().Length == 4)
                                                    {
                                                        horaI = "0" + hI.ToString("HH:mm");
                                                    }
                                                    if (hF.ToString().Length == 4)
                                                    {
                                                        horaF = "0" + hF.ToString("HH:mm");
                                                    }

                                                    sqlInserirAula = "INSERT INTO aula ";
                                                    sqlInserirAula = sqlInserirAula + "(codigoDisciplina, codigoProfessor, horaInicialAula, horaFinalAula, dataAula, codigoAnoLetivo, codigoGrupo, linha, meiaHora, codigoTipoAula)";
                                                    sqlInserirAula = sqlInserirAula + " VALUES (" + lstmateria6.SelectedValue + ", " + lstProfessor.SelectedValue + ", ";
                                                    sqlInserirAula = sqlInserirAula + " '" + horaI + "', '" + horaF + "', '" + data + "', " + ano + ", " + lstgrupo6.SelectedValue + ", '6', 1, 2)";

                                                    cmdInsert = new SqlCommand(sqlInserirAula, sqld);
                                                    cmdInsert.ExecuteNonQuery();
                                                    break;
                                                }
                                            }
                                        }

                                        //mensagem java script
                                        string finalizar = "<script language=JavaScript>finalizar6();</script>";
                                        Panel2.Controls.Add(new LiteralControl(finalizar));

                                        mensagem = false;
                                    }
                                }
                            }
                            else
                            {
                                //validar já existe aula nessas horas

                                //mensagem java script
                                string existe = "<script language=JavaScript>existe6();</script>";
                                Panel2.Controls.Add(new LiteralControl(existe));
                            }
                        }
                    }



                    else
                    {
                        if ((horaInicial != "" && horaFinal != ""))
                        {

                            //mensagem java script
                            string existe = "<script language=JavaScript>alert('As horas inicial e final devem estar no formato hh:mm.');</script>";
                            Panel2.Controls.Add(new LiteralControl(existe));

                            if (horaFinal.Length != 5)
                            {

                                final6Hora6.Focus();

                            }


                            if (horaInicial.Length != 5)
                            {

                                inicial6Hora6.Focus();

                            }


                        }
                    }









                    {
                        string selectDeletar = "select * from aula where dataAula = '" + data + "' and codigoProfessor = " + lstProfessor.SelectedValue + " and linha = 6 and aulaRealizada <> 1";

                        SqlCommand cmd = new SqlCommand(selectDeletar, sqlc);
                        SqlDataReader dr = cmd.ExecuteReader();

                        if (dr.HasRows)
                        {
                            while (dr.Read())
                            {
                                string delete = "delete from aula where codigoAula = " + dr["codigoAula"].ToString();

                                SqlCommand cmdDeletar = new SqlCommand(delete, sqld);
                                cmdDeletar.ExecuteNonQuery();
                            }
                        }

                        string finalizar = "<script language=JavaScript>finalizar6();</script>";
                        Panel2.Controls.Add(new LiteralControl(finalizar));
                    }

                    sqlc.Close();
                    sqld.Close();
                }
            }
        }

        void linha7(string selectVerificar, string data, string ano, string professor, string hora1Inicial, string hora1Final, string hora2Inicial, string hora2Final, string hora3Inicial, string hora3Final, string hora4Inicial, string hora4Final, string hora5Inicial, string hora5Final, string hora6Inicial, string hora6Final, string hora7Inicial, string hora7Final, string hora8Inicial, string hora8Final, bool mensagem)
        {
            SqlConnection sqlc = new SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
            sqlc.Open();
            SqlConnection sqld = new SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
            sqld.Open();

            //INSERT E VALIDAÇÕES REFERENTES A 1ª LINHA DE AGENDA DE AULA
            if (hide7.Value != "0" || lstgrupo7.SelectedValue != "0")
            {
                if (hide7.Value != "0" && lstgrupo7.SelectedValue != "0")
                {
                    //validação selecione um grupo ou um aluno
                    hide7.Value = "0";
                    lstgrupo7.SelectedValue = "0";
                    lstmateria7.SelectedValue = "0";
                    //mensagem java script
                    string alunoGrupo = "<script language=JavaScript>alunoGrupo7();</script>";
                    Panel2.Controls.Add(new LiteralControl(alunoGrupo));
                }
                else
                {
                    string horaInicial = inicial7Hora7.Text;
                    string horaFinal = final7Hora7.Text;


                    if ((horaInicial.Length == 5 && horaFinal.Length == 5))
                    {






                        if (horaInicial.Length == 4)
                        {
                            horaInicial = "0" + horaInicial;
                        }
                        if (horaFinal.Length == 4)
                        {
                            horaFinal = "0" + horaFinal;
                        }

                        TimeSpan dif = Convert.ToDateTime(horaFinal).Subtract(Convert.ToDateTime(horaInicial));

                        if (dif.Hours <= 0)
                        {
                            //validação horas iguais

                            //mensagem java script
                            string horasIguais = "<script language=JavaScript>horasIguais7();</script>";
                            Panel2.Controls.Add(new LiteralControl(horasIguais));
                        }
                        else
                        {
                            bool validarInsert = true;

                            SqlCommand cmdVerificar = new SqlCommand(selectVerificar + " and linha <> '7'", sqlc);
                            SqlDataReader drVerificar = cmdVerificar.ExecuteReader();

                            if (drVerificar.HasRows)
                            {
                                while (drVerificar.Read())
                                {
                                    hora1Inicial = drVerificar["horaInicial1"].ToString();
                                    hora1Final = drVerificar["horaFinal1"].ToString();

                                    hora2Inicial = drVerificar["horaInicial2"].ToString();
                                    hora2Final = drVerificar["horaFinal2"].ToString();

                                    hora3Inicial = drVerificar["horaInicial3"].ToString();
                                    hora3Final = drVerificar["horaFinal3"].ToString();

                                    hora4Inicial = drVerificar["horaInicial4"].ToString();
                                    hora4Final = drVerificar["horaFinal4"].ToString();

                                    hora5Inicial = drVerificar["horaInicial5"].ToString();
                                    hora5Final = drVerificar["horaFinal5"].ToString();

                                    hora6Inicial = drVerificar["horaInicial6"].ToString();
                                    hora6Final = drVerificar["horaFinal6"].ToString();

                                    hora7Inicial = drVerificar["horaInicial7"].ToString();
                                    hora7Final = drVerificar["horaFinal7"].ToString();

                                    hora8Inicial = drVerificar["horaInicial8"].ToString();
                                    hora8Final = drVerificar["horaFinal8"].ToString();


                                    switch (drVerificar["linha"].ToString())
                                    {
                                        case "2":
                                            if (hora2Inicial != "" && hora2Final != "")
                                            {
                                                if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial2"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal2"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial2"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal2"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial2"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal2"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }
                                            }
                                            break;
                                        case "3":
                                            if (hora3Inicial != "" && hora3Final != "")
                                            {
                                                if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial3"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal3"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial3"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal3"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial3"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal3"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }
                                            }
                                            break;
                                        case "4":
                                            if (hora4Inicial != "" && hora4Final != "")
                                            {
                                                if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial4"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal4"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial4"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal4"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial4"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal4"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }
                                            }
                                            break;

                                        case "5":
                                            if (hora5Inicial != "" && hora5Final != "")
                                            {
                                                if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial5"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal5"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial5"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal5"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial5"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal5"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }
                                            }
                                            break;

                                        case "6":
                                            if (hora6Inicial != "" && hora6Final != "")
                                            {
                                                if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial6"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal6"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial6"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal6"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial6"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal6"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }
                                            }
                                            break;

                                        case "1":
                                            if (hora1Inicial != "" && hora1Final != "")
                                            {
                                                if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial1"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal1"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial1"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal1"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial1"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal1"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }
                                            }
                                            break;

                                        case "8":
                                            if (hora8Inicial != "" && hora8Final != "")
                                            {
                                                if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial8"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal8"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial8"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal8"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial8"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal8"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }
                                            }
                                            break;
                                        default:
                                            break;
                                    }
                                }
                            }
                            drVerificar.Close();

                            if (validarInsert)
                            {
                                string sqlInserirAula = "";
                                if (verificarAulaAluno(hide7.Value, lstgrupo7.SelectedValue, data, horaInicial, horaFinal, "7", lstProfessor.SelectedValue))
                                {
                                    if (hide7.Value != "0")
                                    {
                                        DateTime hI = Convert.ToDateTime(horaInicial);

                                        string sqlDeletarAula = "delete from aula where linha = '7' and codigoProfessor = " + lstProfessor.SelectedValue + " and dataAula = '" + data + "'";

                                        SqlCommand cmdDelete = new SqlCommand(sqlDeletarAula, sqld);
                                        cmdDelete.ExecuteNonQuery();
                                        if ((lstmateria7.SelectedValue != "0"))
                                        {
                                            int i = 0;
                                            while (i < dif.TotalHours)
                                            {
                                                DateTime hF = hI;
                                                hF += new TimeSpan(0, 0, 3600);

                                                string horaI = hI.ToString("HH:mm");
                                                string horaF = hF.ToString("HH:mm");

                                                if (hI.ToString().Length == 4)
                                                {
                                                    horaI = "0" + hI.ToString("HH:mm");
                                                }
                                                if (hF.ToString().Length == 4)
                                                {
                                                    horaF = "0" + hF.ToString("HH:mm");
                                                }

                                                sqlInserirAula = "INSERT INTO aula ";
                                                sqlInserirAula = sqlInserirAula + "(codigoDisciplina, codigoProfessor, horaInicialAula, horaFinalAula, dataAula, codigoAluno, codigoAnoLetivo, linha, meiaHora, codigoTipoAula)";
                                                sqlInserirAula = sqlInserirAula + " VALUES (" + lstmateria7.SelectedValue + ", " + lstProfessor.SelectedValue + ", ";
                                                sqlInserirAula = sqlInserirAula + " '" + horaI + "', '" + horaF + "', '" + data + "', " + hide7.Value + ", " + ano + ",'7', 0, 1)";

                                                if (Convert.ToDateTime(horaFinal) != hF)
                                                {
                                                    hI = hF;
                                                }

                                                SqlCommand cmdInsert = new SqlCommand(sqlInserirAula, sqld);
                                                cmdInsert.ExecuteNonQuery();
                                                ///////////////////////////////////////////////////////AQUI
                                                i++;
                                                if ((Convert.ToDouble(dif.TotalHours) - i) == 0.5)
                                                {
                                                    hF = hI;
                                                    hF += new TimeSpan(0, 0, 1800);

                                                    horaI = hI.ToString("HH:mm");
                                                    horaF = hF.ToString("HH:mm");

                                                    if (hI.ToString().Length == 4)
                                                    {
                                                        horaI = "0" + hI.ToString("HH:mm");
                                                    }
                                                    if (hF.ToString().Length == 4)
                                                    {
                                                        horaF = "0" + hF.ToString("HH:mm");
                                                    }

                                                    sqlInserirAula = "INSERT INTO aula ";
                                                    sqlInserirAula = sqlInserirAula + "(codigoDisciplina, codigoProfessor, horaInicialAula, horaFinalAula, dataAula, codigoAluno, codigoAnoLetivo, linha, meiaHora, codigoTipoAula)";
                                                    sqlInserirAula = sqlInserirAula + " VALUES (" + lstmateria7.SelectedValue + ", " + lstProfessor.SelectedValue + ", ";
                                                    sqlInserirAula = sqlInserirAula + " '" + horaI + "', '" + horaF + "', '" + data + "', " + hide7.Value + ", " + ano + ", '7', 1, 1)";

                                                    cmdInsert = new SqlCommand(sqlInserirAula, sqld);
                                                    cmdInsert.ExecuteNonQuery();
                                                    break;
                                                }
                                            }
                                        }

                                        //mensagem java script
                                        string finalizar = "<script language=JavaScript>finalizar7();</script>";
                                        Panel2.Controls.Add(new LiteralControl(finalizar));

                                        mensagem = false;
                                    }
                                    else
                                    {
                                        DateTime hI = Convert.ToDateTime(horaInicial);

                                        string sqlDeletarAula = "delete from aula where linha = '7' and codigoProfessor = " + lstProfessor.SelectedValue + " and dataAula = '" + data + "'";

                                        SqlCommand cmdDelete = new SqlCommand(sqlDeletarAula, sqld);
                                        cmdDelete.ExecuteNonQuery();

                                        if ((lstmateria7.SelectedValue != "0"))
                                        {
                                            int i = 0;
                                            while (i < dif.TotalHours)
                                            {
                                                DateTime hF = hI;
                                                hF += new TimeSpan(0, 0, 3600);

                                                string horaI = hI.ToString("HH:mm");
                                                string horaF = hF.ToString("HH:mm");

                                                if (hI.ToString().Length == 4)
                                                {
                                                    horaI = "0" + hI.ToString("HH:mm");
                                                }
                                                if (hF.ToString().Length == 4)
                                                {
                                                    horaF = "0" + hF.ToString("HH:mm");
                                                }
                                                sqlInserirAula = "INSERT INTO aula ";
                                                sqlInserirAula = sqlInserirAula + "(codigoDisciplina, codigoProfessor, horaInicialAula, horaFinalAula, dataAula, codigoAnoLetivo, codigoGrupo, linha, meiaHora, codigoTipoAula)";
                                                sqlInserirAula = sqlInserirAula + " VALUES (" + lstmateria7.SelectedValue + ", " + lstProfessor.SelectedValue + ", ";
                                                sqlInserirAula = sqlInserirAula + " '" + horaI + "', '" + horaF + "', '" + data + "', " + ano + ", " + lstgrupo7.SelectedValue + ", '7', 0, 2)";

                                                if (Convert.ToDateTime(horaFinal) != hF)
                                                {
                                                    hI = hF;
                                                }

                                                SqlCommand cmdInsert = new SqlCommand(sqlInserirAula, sqld);
                                                cmdInsert.ExecuteNonQuery();

                                                i++;
                                                if ((Convert.ToDouble(dif.TotalHours) - i) == 0.5)
                                                {
                                                    hF = hI;
                                                    hF += new TimeSpan(0, 0, 1800);

                                                    horaI = hI.ToString("HH:mm");
                                                    horaF = hF.ToString("HH:mm");

                                                    if (hI.ToString().Length == 4)
                                                    {
                                                        horaI = "0" + hI.ToString("HH:mm");
                                                    }
                                                    if (hF.ToString().Length == 4)
                                                    {
                                                        horaF = "0" + hF.ToString("HH:mm");
                                                    }

                                                    sqlInserirAula = "INSERT INTO aula ";
                                                    sqlInserirAula = sqlInserirAula + "(codigoDisciplina, codigoProfessor, horaInicialAula, horaFinalAula, dataAula, codigoAnoLetivo, codigoGrupo, linha, meiaHora, codigoTipoAula)";
                                                    sqlInserirAula = sqlInserirAula + " VALUES (" + lstmateria7.SelectedValue + ", " + lstProfessor.SelectedValue + ", ";
                                                    sqlInserirAula = sqlInserirAula + " '" + horaI + "', '" + horaF + "', '" + data + "', " + ano + ", " + lstgrupo7.SelectedValue + ",'7', 1, 2)";

                                                    cmdInsert = new SqlCommand(sqlInserirAula, sqld);
                                                    cmdInsert.ExecuteNonQuery();
                                                    break;
                                                }
                                            }
                                        }

                                        //mensagem java script
                                        string finalizar = "<script language=JavaScript>finalizar7();</script>";
                                        Panel2.Controls.Add(new LiteralControl(finalizar));

                                        mensagem = false;
                                    }
                                }
                            }
                            else
                            {
                                //validar já existe aula nessas horas

                                //mensagem java script
                                string existe = "<script language=JavaScript>existe7();</script>";
                                Panel2.Controls.Add(new LiteralControl(existe));
                            }
                        }
                    }


                    else
                    {

                        if ((horaInicial != "" && horaFinal != ""))
                        {
                            //mensagem java script
                            string existe = "<script language=JavaScript>alert('As horas inicial e final devem estar no formato hh:mm.');</script>";
                            Panel2.Controls.Add(new LiteralControl(existe));

                            if (horaFinal.Length != 5)
                            {

                                final7Hora7.Focus();

                            }


                            if (horaInicial.Length != 5)
                            {

                                inicial7Hora7.Focus();

                            }


                        }
                    }









                    {
                        string selectDeletar = "select * from aula where dataAula = '" + data + "' and codigoProfessor = " + lstProfessor.SelectedValue + " and linha = 7 and aulaRealizada <> 1";

                        SqlCommand cmd = new SqlCommand(selectDeletar, sqlc);
                        SqlDataReader dr = cmd.ExecuteReader();

                        if (dr.HasRows)
                        {
                            while (dr.Read())
                            {
                                string delete = "delete from aula where codigoAula = " + dr["codigoAula"].ToString();

                                SqlCommand cmdDeletar = new SqlCommand(delete, sqld);
                                cmdDeletar.ExecuteNonQuery();
                            }
                        }

                        string finalizar = "<script language=JavaScript>finalizar7();</script>";
                        Panel2.Controls.Add(new LiteralControl(finalizar));
                    }

                    sqlc.Close();
                    sqld.Close();
                }
            }
        }

        void linha8(string selectVerificar, string data, string ano, string professor, string hora1Inicial, string hora1Final, string hora2Inicial, string hora2Final, string hora3Inicial, string hora3Final, string hora4Inicial, string hora4Final, string hora5Inicial, string hora5Final, string hora6Inicial, string hora6Final, string hora7Inicial, string hora7Final, string hora8Inicial, string hora8Final, bool mensagem)
        {
            SqlConnection sqlc = new SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
            sqlc.Open();
            SqlConnection sqld = new SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
            sqld.Open();

            //INSERT E VALIDAÇÕES REFERENTES A 1ª LINHA DE AGENDA DE AULA
            if (hide8.Value != "0" || lstgrupo8.SelectedValue != "0")
            {
                if (hide8.Value != "0" && lstgrupo8.SelectedValue != "0")
                {
                    //validação selecione um grupo ou um aluno
                    hide8.Value = "0";
                    lstgrupo8.SelectedValue = "0";
                    lstmateria8.SelectedValue = "0";
                    //mensagem java script
                    string alunoGrupo = "<script language=JavaScript>alunoGrupo8();</script>";
                    Panel2.Controls.Add(new LiteralControl(alunoGrupo));
                }
                else
                {
                    string horaInicial = inicial8Hora8.Text;
                    string horaFinal = final8Hora8.Text;



                    if ((horaInicial.Length == 5 && horaFinal.Length == 5))
                    {







                        if (horaInicial.Length == 4)
                        {
                            horaInicial = "0" + horaInicial;
                        }
                        if (horaFinal.Length == 4)
                        {
                            horaFinal = "0" + horaFinal;
                        }

                        TimeSpan dif = Convert.ToDateTime(horaFinal).Subtract(Convert.ToDateTime(horaInicial));

                        if (dif.Hours <= 0)
                        {
                            //validação horas iguais

                            //mensagem java script
                            string horasIguais = "<script language=JavaScript>horasIguais8();</script>";
                            Panel2.Controls.Add(new LiteralControl(horasIguais));
                        }
                        else
                        {
                            bool validarInsert = true;

                            SqlCommand cmdVerificar = new SqlCommand(selectVerificar + " and linha <> '8'", sqlc);
                            SqlDataReader drVerificar = cmdVerificar.ExecuteReader();

                            if (drVerificar.HasRows)
                            {
                                while (drVerificar.Read())
                                {
                                    hora1Inicial = drVerificar["horaInicial1"].ToString();
                                    hora1Final = drVerificar["horaFinal1"].ToString();

                                    hora2Inicial = drVerificar["horaInicial2"].ToString();
                                    hora2Final = drVerificar["horaFinal2"].ToString();

                                    hora3Inicial = drVerificar["horaInicial3"].ToString();
                                    hora3Final = drVerificar["horaFinal3"].ToString();

                                    hora4Inicial = drVerificar["horaInicial4"].ToString();
                                    hora4Final = drVerificar["horaFinal4"].ToString();

                                    hora5Inicial = drVerificar["horaInicial5"].ToString();
                                    hora5Final = drVerificar["horaFinal5"].ToString();

                                    hora6Inicial = drVerificar["horaInicial6"].ToString();
                                    hora6Final = drVerificar["horaFinal6"].ToString();

                                    hora7Inicial = drVerificar["horaInicial7"].ToString();
                                    hora7Final = drVerificar["horaFinal7"].ToString();

                                    hora8Inicial = drVerificar["horaInicial8"].ToString();
                                    hora8Final = drVerificar["horaFinal8"].ToString();


                                    switch (drVerificar["linha"].ToString())
                                    {
                                        case "2":
                                            if (hora2Inicial != "" && hora2Final != "")
                                            {
                                                if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial2"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal2"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial2"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal2"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial2"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal2"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }
                                            }
                                            break;
                                        case "3":
                                            if (hora3Inicial != "" && hora3Final != "")
                                            {
                                                if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial3"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal3"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial3"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal3"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial3"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal3"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }
                                            }
                                            break;
                                        case "4":
                                            if (hora4Inicial != "" && hora4Final != "")
                                            {
                                                if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial4"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal4"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial4"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal4"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial4"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal4"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }
                                            }
                                            break;

                                        case "5":
                                            if (hora5Inicial != "" && hora5Final != "")
                                            {
                                                if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial5"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal5"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial5"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal5"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial5"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal5"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }
                                            }
                                            break;

                                        case "6":
                                            if (hora6Inicial != "" && hora6Final != "")
                                            {
                                                if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial6"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal6"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial6"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal6"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial6"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal6"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }
                                            }
                                            break;

                                        case "7":
                                            if (hora7Inicial != "" && hora7Final != "")
                                            {
                                                if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial7"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal7"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial7"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal7"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial7"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal7"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }
                                            }
                                            break;

                                        case "1":
                                            if (hora1Inicial != "" && hora1Final != "")
                                            {
                                                if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial1"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal1"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial1"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal1"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }

                                                if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial1"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal1"].ToString()))
                                                {
                                                    validarInsert = false;
                                                    break;
                                                }
                                            }
                                            break;
                                        default:
                                            break;
                                    }
                                }
                            }
                            drVerificar.Close();

                            if (validarInsert)
                            {
                                string sqlInserirAula = "";
                                if (verificarAulaAluno(hide8.Value, lstgrupo8.SelectedValue, data, horaInicial, horaFinal, "8", lstProfessor.SelectedValue))
                                {
                                    if (hide8.Value != "0")
                                    {
                                        DateTime hI = Convert.ToDateTime(horaInicial);

                                        string sqlDeletarAula = "delete from aula where linha = '8' and codigoProfessor = " + lstProfessor.SelectedValue + " and dataAula = '" + data + "'";

                                        SqlCommand cmdDelete = new SqlCommand(sqlDeletarAula, sqld);
                                        cmdDelete.ExecuteNonQuery();
                                        if ((lstmateria8.SelectedValue != "0"))
                                        {
                                            int i = 0;
                                            while (i < dif.TotalHours)
                                            {
                                                DateTime hF = hI;
                                                hF += new TimeSpan(0, 0, 3600);

                                                string horaI = hI.ToString("HH:mm");
                                                string horaF = hF.ToString("HH:mm");

                                                if (hI.ToString().Length == 4)
                                                {
                                                    horaI = "0" + hI.ToString("HH:mm");
                                                }
                                                if (hF.ToString().Length == 4)
                                                {
                                                    horaF = "0" + hF.ToString("HH:mm");
                                                }

                                                sqlInserirAula = "INSERT INTO aula ";
                                                sqlInserirAula = sqlInserirAula + "(codigoDisciplina, codigoProfessor, horaInicialAula, horaFinalAula, dataAula, codigoAluno, codigoAnoLetivo, linha, meiaHora, codigoTipoAula)";
                                                sqlInserirAula = sqlInserirAula + " VALUES (" + lstmateria8.SelectedValue + ", " + lstProfessor.SelectedValue + ", ";
                                                sqlInserirAula = sqlInserirAula + " '" + horaI + "', '" + horaF + "', '" + data + "', " + hide8.Value + ", " + ano + ", '8', 0, 1)";

                                                if (Convert.ToDateTime(horaFinal) != hF)
                                                {
                                                    hI = hF;
                                                }

                                                SqlCommand cmdInsert = new SqlCommand(sqlInserirAula, sqld);
                                                cmdInsert.ExecuteNonQuery();
                                                ///////////////////////////////////////////////////////AQUI
                                                i++;
                                                if ((Convert.ToDouble(dif.TotalHours) - i) == 0.5)
                                                {
                                                    hF = hI;
                                                    hF += new TimeSpan(0, 0, 1800);

                                                    horaI = hI.ToString("HH:mm");
                                                    horaF = hF.ToString("HH:mm");

                                                    if (hI.ToString().Length == 4)
                                                    {
                                                        horaI = "0" + hI.ToString("HH:mm");
                                                    }
                                                    if (hF.ToString().Length == 4)
                                                    {
                                                        horaF = "0" + hF.ToString("HH:mm");
                                                    }

                                                    sqlInserirAula = "INSERT INTO aula ";
                                                    sqlInserirAula = sqlInserirAula + "(codigoDisciplina, codigoProfessor, horaInicialAula, horaFinalAula, dataAula, codigoAluno, codigoAnoLetivo, linha, meiaHora, codigoTipoAula)";
                                                    sqlInserirAula = sqlInserirAula + " VALUES (" + lstmateria8.SelectedValue + ", " + lstProfessor.SelectedValue + ", ";
                                                    sqlInserirAula = sqlInserirAula + " '" + horaI + "', '" + horaF + "', '" + data + "', " + hide8.Value + ", " + ano + ", '8', 1, 1)";

                                                    cmdInsert = new SqlCommand(sqlInserirAula, sqld);
                                                    cmdInsert.ExecuteNonQuery();
                                                    break;
                                                }
                                            }
                                        }

                                        //mensagem java script
                                        string finalizar = "<script language=JavaScript>finalizar8();</script>";
                                        Panel2.Controls.Add(new LiteralControl(finalizar));

                                        mensagem = false;
                                    }
                                    else
                                    {
                                        DateTime hI = Convert.ToDateTime(horaInicial);

                                        string sqlDeletarAula = "delete from aula where linha = '8' and codigoProfessor = " + lstProfessor.SelectedValue + " and dataAula = '" + data + "'";

                                        SqlCommand cmdDelete = new SqlCommand(sqlDeletarAula, sqld);
                                        cmdDelete.ExecuteNonQuery();

                                        if ((lstmateria8.SelectedValue != "0"))
                                        {
                                            int i = 0;
                                            while (i < dif.TotalHours)
                                            {
                                                DateTime hF = hI;
                                                hF += new TimeSpan(0, 0, 3600);

                                                string horaI = hI.ToString("HH:mm");
                                                string horaF = hF.ToString("HH:mm");

                                                if (hI.ToString().Length == 4)
                                                {
                                                    horaI = "0" + hI.ToString("HH:mm");
                                                }
                                                if (hF.ToString().Length == 4)
                                                {
                                                    horaF = "0" + hF.ToString("HH:mm");
                                                }
                                                sqlInserirAula = "INSERT INTO aula ";
                                                sqlInserirAula = sqlInserirAula + "(codigoDisciplina, codigoProfessor, horaInicialAula, horaFinalAula, dataAula, codigoAnoLetivo, codigoGrupo, linha, meiaHora, codigoTipoAula)";
                                                sqlInserirAula = sqlInserirAula + " VALUES (" + lstmateria8.SelectedValue + ", " + lstProfessor.SelectedValue + ", ";
                                                sqlInserirAula = sqlInserirAula + " '" + horaI + "', '" + horaF + "', '" + data + "', " + ano + ", " + lstgrupo8.SelectedValue + ", '8', 0, 2)";

                                                if (Convert.ToDateTime(horaFinal) != hF)
                                                {
                                                    hI = hF;
                                                }

                                                SqlCommand cmdInsert = new SqlCommand(sqlInserirAula, sqld);
                                                cmdInsert.ExecuteNonQuery();

                                                i++;
                                                if ((Convert.ToDouble(dif.TotalHours) - i) == 0.5)
                                                {
                                                    hF = hI;
                                                    hF += new TimeSpan(0, 0, 1800);

                                                    horaI = hI.ToString("HH:mm");
                                                    horaF = hF.ToString("HH:mm");

                                                    if (hI.ToString().Length == 4)
                                                    {
                                                        horaI = "0" + hI.ToString("HH:mm");
                                                    }
                                                    if (hF.ToString().Length == 4)
                                                    {
                                                        horaF = "0" + hF.ToString("HH:mm");
                                                    }

                                                    sqlInserirAula = "INSERT INTO aula ";
                                                    sqlInserirAula = sqlInserirAula + "(codigoDisciplina, codigoProfessor, horaInicialAula, horaFinalAula, dataAula, codigoAnoLetivo, codigoGrupo, linha, meiaHora, codigoTipoAula)";
                                                    sqlInserirAula = sqlInserirAula + " VALUES (" + lstmateria8.SelectedValue + ", " + lstProfessor.SelectedValue + ", ";
                                                    sqlInserirAula = sqlInserirAula + " '" + horaI + "', '" + horaF + "', '" + data + "', " + ano + ", " + lstgrupo8.SelectedValue + ", '8', 1, 2)";

                                                    cmdInsert = new SqlCommand(sqlInserirAula, sqld);
                                                    cmdInsert.ExecuteNonQuery();
                                                    break;
                                                }
                                            }
                                        }

                                        //mensagem java script
                                        string finalizar = "<script language=JavaScript>finalizar8();</script>";
                                        Panel2.Controls.Add(new LiteralControl(finalizar));

                                        mensagem = false;
                                    }
                                }
                            }
                            else
                            {
                                //validar já existe aula nessas horas

                                //mensagem java script
                                string existe = "<script language=JavaScript>existe8();</script>";
                                Panel2.Controls.Add(new LiteralControl(existe));
                            }
                        }
                    }


                    else
                    {

                        if ((horaInicial != "" && horaFinal != ""))
                        {
                            //mensagem java script
                            string existe = "<script language=JavaScript>alert('As horas inicial e final devem estar no formato hh:mm.');</script>";
                            Panel2.Controls.Add(new LiteralControl(existe));

                            if (horaFinal.Length != 5)
                            {

                                final8Hora8.Focus();

                            }


                            if (horaInicial.Length != 5)
                            {

                                inicial8Hora8.Focus();

                            }


                        }
                    }















                    {
                        string selectDeletar = "select * from aula where dataAula = '" + data + "' and codigoProfessor = " + lstProfessor.SelectedValue + " and linha = 8 and aulaRealizada <> 1";

                        SqlCommand cmd = new SqlCommand(selectDeletar, sqlc);
                        SqlDataReader dr = cmd.ExecuteReader();

                        if (dr.HasRows)
                        {
                            while (dr.Read())
                            {
                                string delete = "delete from aula where codigoAula = " + dr["codigoAula"].ToString();

                                SqlCommand cmdDeletar = new SqlCommand(delete, sqld);
                                cmdDeletar.ExecuteNonQuery();
                            }
                        }

                        string finalizar = "<script language=JavaScript>finalizar8();</script>";
                        Panel2.Controls.Add(new LiteralControl(finalizar));
                    }

                    sqlc.Close();
                    sqld.Close();
                }
            }
        }

        bool verificarAulaAluno(string codigoAluno, string codigoGrupo, string dataAula, string horaInicial, string horaFinal, string linha, string codigoProfessor)
        {
            SqlConnection sqlc = new SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
            sqlc.Open();

            string select = "";

            if (codigoAluno != "" && codigoAluno != "0")
            {
                select = "select aula.*, ";

                select += "(SELECT MIN(horaInicialAula) AS horaInicial FROM aula AS a WHERE (codigoProfessor = aula.codigoProfessor) AND (codigoAluno = aula.codigoAluno) AND (dataAula = aula.dataAula)) AS horaInicialM, ";
                select += "(SELECT MIN(horaInicialAula) AS horaInicial FROM aula AS a WHERE (codigoProfessor = aula.codigoProfessor) AND (codigoAluno = aula.codigoAluno) AND (dataAula = aula.dataAula)) AS horaFinalM ";

                select += "from aula where codigoProfessor <> " + codigoProfessor + " and dataAula = '" + dataAula + "' and codigoAluno = " + codigoAluno;
            }
            else
            {
                select = "select aula.*, ";

                select += "(SELECT MIN(horaInicialAula) AS horaInicial FROM aula AS a WHERE (codigoProfessor = aula.codigoProfessor) AND (codigoAluno = aula.codigoAluno) AND (dataAula = aula.dataAula)) AS horaInicialM, ";
                select += "(SELECT MAX(horaFinalAula) AS horaFinal FROM aula AS aa WHERE (codigoGrupo = aula.codigoGrupo) AND (dataAula = aula.dataAula)) AS horaFinalM ";

                select += "from aula where codigoProfessor <> " + codigoProfessor + " and dataAula = '" + dataAula + "' and codigoGrupo = " + codigoGrupo;
            }

            SqlCommand cmd = new SqlCommand(select, sqlc);
            SqlDataReader dr = cmd.ExecuteReader();

            string horaIncialLooping, horaFinalLooping, msg;

            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    horaIncialLooping = dr["horaInicialM"].ToString();
                    horaFinalLooping = dr["horaFinalM"].ToString();

                    //VERIFICAR SE O INTERVALOS DE HORAS JÁ CADASTRADOS É IGUAL A QUE O USUÁRIO PRETENDE INSERIR
                    // CASO SEJA IGUAL REPRESENTA QUE O ALUNO JÁ POSSUI UM CRONOGRAMA PARA ESSE HORÁRIO
                    if (Convert.ToDateTime(horaInicial) > Convert.ToDateTime(horaIncialLooping) &&
                        Convert.ToDateTime(horaInicial) < Convert.ToDateTime(horaFinalLooping))
                    {
                        msg = "<script language=\"javascript\">alert('O aluno já possui aula agendada neste horário. (Linha " + linha + ")');</script>";
                        Panel2.Controls.Add(new LiteralControl(msg));
                        dr.Close();

                        sqlc.Close();
                        return false;
                    }

                    if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(horaIncialLooping) &&
                        Convert.ToDateTime(horaFinal) < Convert.ToDateTime(horaFinalLooping))
                    {
                        msg = "<script language=\"javascript\">alert('O grupo já possui aula agendada neste horário. (Linha " + linha + ")');</script>";
                        Panel2.Controls.Add(new LiteralControl(msg));
                        dr.Close();

                        sqlc.Close();
                        return false;
                    }
                }
            }

            dr.Close();

            sqlc.Close();
            return true;
        }

        public string statusVer(string codigoAluno)
        {
            double v = pegarValorTotal(codigoAluno);

            if (pegarValorTotal(codigoAluno) < 0)
            {
                return "sim";
            }
            else
            {
                return "não";
            }
        }

        double pegarValorTotal(string codigoAluno)
        {
            SqlConnection sqlc = new SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
            sqlc.Open();

            string selectAula = "SELECT a.codigoAula, a.codigoProfessor, a.codigoTipoAula, ";
            selectAula += "(select nomeProfessor from professor where a.codigoProfessor = professor.codigoProfessor) as nomeProfessor, ";

            selectAula += "(select nomeAluno from aluno where a.codigoAluno = aluno.codigoAluno) as nomeAluno, ";

            selectAula += "a.codigoAluno, a.codigoGrupo, a.dataAula, a.horaInicialAula, a.horaFinalAula, a.codigoDisciplina, a.descricaoAssuntoAula, ";
            selectAula += "a.aulaRealizada, a.observacoesAula, a.valorHora, a.pago, a.aulaCancelada, a.valorPago, ";
            selectAula += "a.valorTotalSemDesconto, a.valorTotalComDesconto, a.codigoAnoLetivo, a.falta, a.meiaHora, ";

            selectAula += "(select v.aulaValor from valorAula v ";
            selectAula += "where convert(datetime,a.dataAula,103) BETWEEN convert(datetime, v.dataInicial,103) ";
            selectAula += "AND convert(datetime, v.dataFinal,103) and a.codigoTipoAula = v.codigoTipoAula) as valorAula, ";

            selectAula += "(select v.valorValorEspecialAluno from valorEspecialAluno v ";
            selectAula += "where convert(datetime,a.dataAula,103) BETWEEN convert(datetime, v.dataInicialValorEspecialAluno,103) ";
            selectAula += "AND convert(datetime, v.dataFinalValorEspecialAluno,103) and a.codigoAluno = v.codigoAluno) as valorAulaEspecial ";

            selectAula += "FROM aula a ";
            selectAula += "WHERE a.aulaRealizada = 1 and a.codigoAluno = " + codigoAluno;
            selectAula += " order by convert(datetime,a.dataAula,103) desc, CONVERT(INT, SUBSTRING(a.horaInicialAula,1 , 2))  ";

            SqlCommand cmdPrincipal = new SqlCommand(selectAula, sqlc);
            SqlDataReader drPrincipal = cmdPrincipal.ExecuteReader();

            double valorTotal = 0;

            if (drPrincipal.HasRows)
            {
                while (drPrincipal.Read())
                {
                    if (drPrincipal["valorAulaEspecial"].ToString().Replace(" ", "") != "")
                    {
                        try
                        {
                            valorTotal += Convert.ToDouble(drPrincipal["valorAulaEspecial"].ToString());
                        }
                        catch
                        {
                        }
                    }
                    else
                    {
                        try
                        {
                            valorTotal += Convert.ToDouble(drPrincipal["valorAula"].ToString());
                        }
                        catch
                        {
                        }
                    }
                }
            }

            drPrincipal.Close();

            sqlc.Close();

            return pegarValorPago(codigoAluno) - (valorTotal + pegarValorTotalGrupo(codigoAluno));
        }

        double pegarValorTotalGrupo(string codigoAluno)
        {
            double valor = 0;

            string select = "select * from alunoGrupo where codigoAluno = " + codigoAluno + "";

            SqlConnection sqlc = new SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
            sqlc.Open();
            SqlConnection sqld = new SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
            sqld.Open();

            SqlCommand cmdAlunoGrupo = new SqlCommand(select, sqlc);
            SqlDataReader myReaderAlunoGrupo = cmdAlunoGrupo.ExecuteReader();

            if (myReaderAlunoGrupo.HasRows)
            {
                while (myReaderAlunoGrupo.Read())
                {
                    string selectAula = "SELECT a.codigoAula, a.codigoProfessor, a.codigoTipoAula, ";
                    selectAula += "(select nomeProfessor from professor where a.codigoProfessor = professor.codigoProfessor) as nomeProfessor, ";

                    selectAula += "(select nomeAluno from aluno where aluno.codigoAluno = " + codigoAluno + ") as nomeAluno, ";

                    selectAula += "a.codigoAluno, a.codigoGrupo, a.dataAula, a.horaInicialAula, a.horaFinalAula, a.codigoDisciplina, a.descricaoAssuntoAula, ";
                    selectAula += "a.aulaRealizada, a.observacoesAula, a.valorHora, a.pago, a.aulaCancelada, a.valorPago, ";
                    selectAula += "a.valorTotalSemDesconto, a.valorTotalComDesconto, a.codigoAnoLetivo, a.falta, a.meiaHora, ";

                    selectAula += "(select v.aulaValor from valorAula v ";
                    selectAula += "where convert(datetime,a.dataAula,103) BETWEEN convert(datetime, v.dataInicial,103) ";
                    selectAula += "AND convert(datetime, v.dataFinal,103) and a.codigoTipoAula = v.codigoTipoAula) as valorAula, ";

                    selectAula += "(select v.valorValorEspecialAluno from valorEspecialAluno v ";
                    selectAula += "where convert(datetime,a.dataAula,103) BETWEEN convert(datetime, v.dataInicialValorEspecialAluno,103) ";
                    selectAula += "AND convert(datetime, v.dataFinalValorEspecialAluno,103) and v.codigoAluno = " + codigoAluno + ") as valorAulaEspecial ";

                    selectAula += "FROM aula a ";
                    selectAula += "WHERE a.codigoGrupo = " + myReaderAlunoGrupo["codigoGrupo"].ToString() + " and a.aulaRealizada = 1 ";
                    selectAula += " order by convert(datetime,a.dataAula,103) desc, CONVERT(INT, SUBSTRING(a.horaInicialAula,1 , 2))  ";

                    SqlCommand cmdPrincipal = new SqlCommand(selectAula, sqld);
                    SqlDataReader drPrincipal = cmdPrincipal.ExecuteReader();

                    if (drPrincipal.HasRows)
                    {
                        while (drPrincipal.Read())
                        {
                            if (drPrincipal["valorAulaEspecial"].ToString().Replace(" ", "") != "")
                            {
                                try
                                {
                                    valor += Convert.ToDouble(drPrincipal["valorAulaEspecial"].ToString());
                                }
                                catch
                                {
                                }
                            }
                            else
                            {
                                try
                                {
                                    valor += Convert.ToDouble(drPrincipal["valorAula"].ToString());
                                }
                                catch
                                {
                                }
                            }
                        }
                    }
                    drPrincipal.Close();
                }
            }

            cmdAlunoGrupo.Dispose();
            myReaderAlunoGrupo.Close();
            sqlc.Close();
            sqld.Close();

            return valor;
        }

        double pegarValorPago(string codigoAluno)
        {
            double valorPago = 0;

            SqlConnection sqlc = new SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
            sqlc.Open();

            string select = "select * from pagamento where codigoAluno = " + codigoAluno;

            SqlCommand cmd = new SqlCommand(select, sqlc);
            SqlDataReader dr = cmd.ExecuteReader();

            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    valorPago += Convert.ToDouble(dr["valor"].ToString());
                }
            }

            dr.Close();

            sqlc.Close();

            return valorPago;
        }
    }
}
